初级程序员;程序在开关上立即关闭 (C++)
我一直在摆弄我的非常基本的 C++ 知识(编程了两天),试图编写一个计算 phi (1.61803399) 周围的用户输入的程序。
这是代码,如果代码混乱,抱歉:
#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();
}
无论如何,在第一个提示(1-4)输入数字时,程序只是崩溃到桌面,我不明白为什么。
任何帮助将不胜感激。
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).
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.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你确定它会崩溃吗?该代码仅返回操作的值(转换为 int,因为这是
main
的返回类型)。我建议您应该使用cout << 打印它opt4f()。
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
). I would suggest that you should print it usingcout << opt4f()
.问题出在 switch 中的 return 语句上。
函数
main()
的特殊之处在于,它的返回值告诉操作系统程序是否成功。如果main()
的返回值为 0,则一切正常。如果它不为零,则存在错误。在您的代码中,您将返回
opt1f(phi, userInput)
、optef(phi, userInput)
等的值。这些值可能非零,因此表明您的程序失败的操作系统。The problem comes with the return statements in your switch.
The function
main()
is special in that the return value tells the operating system whether the program was successful. If the return value ofmain()
is 0, then everything worked fine. 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.当我运行你的程序时,它不会在 switch 语句上关闭;我收到你的第二条短信。
不过,我看到了一些问题:
首先,正如更快的人所指出的那样,您的返回退出应用程序而不是输出答案。
其次,在将代码更改为 cout 而不是 return 后,您将需要添加“break;”因此,您不必在当前条件之后的每种条件下都运行代码。
第三,您可能想将 goto 更改为输入循环,并向菜单添加退出选项。这更多的是一种风格选择,但我想你会发现 c/c++ 中的 goto 将来更难调试。
-编辑:用于格式化-
好吧,假设您希望每个程序运行时能够执行不止一件事,并且要摆脱 goto,您可以执行以下操作:
Your program doesn't close on the switch statement when I run it; 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;" 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. 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.
-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:
在这种情况下,
return
退出 main() 函数,从而导致程序干净终止。In this case, the
return
s exit the main() function, leading to a clean program termination.程序不会崩溃,它会退出,因为这就是它应该做的。
return
语句意味着执行将退出当前函数,如果当前函数是main
- 它将退出程序。返回值是程序将返回给操作系统的值。如果它不是 0 - 操作系统会认为程序异常退出,但在你的情况下 - 这只是计算的值。
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 beingmain
- it will exit the program.The value of the return is the value which the program will return to the 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.