简体   繁体   English

初级程序员; 程序立即在开关上关闭(C ++)

[英]Beginner programmer; program instantly closes on switch (C++)

I've been messing around with my very basic C++ knowledge (been programming for two days), attempting to write a program that calculates user input around phi (1.61803399). 我一直在瞎搞我非常基本的C ++知识(被编程为2天),试图写入计算各地岛(1.61803399)用户输入的程序。

Here's the code, apologies if its a mess: 这是代码,如果一团糟,我们深表歉意:

#include <iostream>
#include <math.h>

using namespace std;

//Prototypes:

float phiExpo;
float phiNegExpo;    

float opt1f(float phi, float userInput){
  return userInput * phi;}    

float opt2f(float phi, float userInput){
  return userInput / phi;}  

float opt3f(){
  return phiExpo;}   

float opt4f(){
  return phiNegExpo;}   

float phiExpof(float phi, float userInput){
  pow(phi,userInput);}

float phiNegExpof(float phi, float userInput){
  pow(phi,-userInput);}


//Execute program:

int main(){
  float userInput;
  int userChoice;
  float phi = 1.61803399;
  float phiExpo;
  float phiNegExpo;    


cout<<"I want to (press corresponding number, then enter):"<<endl;    
cout<<endl;

startchoices:    
cout<<"1. Multiply by phi:"<<endl;
cout<<"2. Divide by phi:"<<endl;
cout<<"3. Exponentiate phi:"<<endl;
cout<<"4. Negatively exponentiate phi:"<<endl;    
cout<<endl;



cin>>userChoice;  
cout<<endl;      
switch (userChoice){
       case 1:
       cout<<"Enter number for multiplication: ";
       cin>>userInput;
       return opt1f(phi, userInput);
       case 2:
       cout<<"Enter number for division: ";
       cin>>userInput;
       return opt2f(phi, userInput);                
       case 3:
       cout<<"Enter number for to exponetiate phi by: ";
       cin>>userInput;
       return opt3f();
       case 4:
       cout<<"Enter number for negatively exponentiate phi by: ";
       cin>>userInput;     
       return opt4f();           
       default:
       cout<<"Please enter a number from 1 to 4.";
       cout<<endl;
       cout<<endl;
       goto startchoices;
       }



cin.get();


}

Anyway, upon entering a number at the first prompt (1-4), the program simply crashes to the desktop, and I can't figure out why. 无论如何,在第一个提示(1-4)处输入数字后,程序只是崩溃到了桌面上,我不知道为什么。

Any help would be much appreciated. 任何帮助将非常感激。

Are you sure it crashes? 您确定它崩溃了吗? The code simply returns the value of the operation (casted to an int, as this is the return type of main ). 该代码仅返回操作的值(强制转换为int,因为这是main的返回类型)。 I would suggest that you should print it using cout << opt4f() . 我建议您使用cout << opt4f()打印它。

The problem comes with the return statements in your switch. 问题出在交换机中的return语句中。

The function main() is special in that the return value tells the operating system whether the program was successful. 函数main()的特殊之处在于返回值告诉操作系统程序是否成功。 If the return value of main() is 0, then everything worked fine. 如果main()的返回值为0,则一切正常。 If it is nonzero, then there was an error. 如果非零,则有错误。

In your code, you are returning the value of opt1f(phi, userInput) , optef(phi, userInput) , etc. These values are likely non-zero, thus telling the operating system that your program failed. 在您的代码中,您将返回opt1f(phi, userInput)optef(phi, userInput)等的值。这些值很可能非零,从而告诉操作系统您的程序失败了。

在这种情况下, return退出main()函数,从而导致干净的程序终止。

Your program doesn't close on the switch statement when I run it; 当我运行它时,您的程序没有在switch语句上关闭; I get your second text. 我得到你的第二条短信。 There are a few problems I see though: 我有一些问题:

First, as faster people have noted, your returns quit out of the application instead of outputting the answer. 首先,正如更快的人注意到的那样,您的退货将退出应用程序,而不是输出答案。

Secondly, after you change your code to cout rather than return, you will want to put in a "break;" 其次,在将代码更改为cout而不是返回之后,您将需要放置一个“ break”; so you don't run the code in every condition after the current one. 因此,您不必在当前条件之后的所有条件下都运行代码。

Thirdly, you might want to change the goto into an input loop and add a quit option to your menu. 第三,您可能希望将goto更改为输入循环,并在菜单中添加quit选项。 This is more of a stylistic choice, but I think you will find that goto's in c/c++ are harder to debug in the future. 这更多是一种风格选择,但我认为您会发现c / c ++中的goto将来更难调试。

-edit: for formatting- Well, assuming you wanted to be able to do more than one thing per program run, and to get rid of the goto, you could do something like: -edit:用于格式化-好吧,假设您希望每次程序运行都可以做一件以上的事情,并且想要摆脱goto,您可以执行以下操作:

boolean quitting = false;
do {
   cout << "1) Menu item 1" << endl << "2) Quit" << endl;
   cin.get(userchoice);
   switch(userchoice) {
      case 1:
         cout << "Please enter input for option 1: ";
         cin >> userInput;
         cout << case1function(userInput);
         break;
      case 2:
         quitting = true;
         break;
      default:
         cout << "Please read menu" << endl;
   }
}while (!quitting);

The program doesn't crash, it exits because that's what it is supposed to do. 该程序不会崩溃,它会退出,因为这是应该执行的操作。 The return statement means that the execution will exit the current function, and in case of the current function being main - it will exit the program. return语句表示执行将退出当前函数,并且在当前函数为main情况下-它将退出程序。

The value of the return is the value which the program will return to the OS. 返回值是程序将返回到OS的值。 If it's not 0 - the OS will think the program exited abnormally, but in your case - it's just the value of the calculation. 如果它不为0,则OS会认为程序异常退出,但对于您而言,它只是计算的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM