C++ 退出功能不工作
我试图在我的程序中添加一个“退出”函数,但输入它不起作用。输入“退出”后,程序将正常重新启动。我怎样才能改变这个?我试过固定支架之类的东西,但到目前为止,毫无效果C++ 退出功能不工作,c++,C++,我试图在我的程序中添加一个“退出”函数,但输入它不起作用。输入“退出”后,程序将正常重新启动。我怎样才能改变这个?我试过固定支架之类的东西,但到目前为止,毫无效果 #include <iostream> using namespace std; int main(int argc, const char * argv[]) { int sa; sa=2; int sr; sr=0; if (sa != 0) sa=1;
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int sa;
sa=2;
int sr;
sr=0;
if (sa != 0)
sa=1;
string answer;
while (sa == 1){
if (sa == 1)
cout<<"Welcome to:";
cin.ignore();
cout<<"Brendan Quiz Game!";
cin.ignore();
cout<<"Be excited.";
cin.ignore();
cout<<"Enter HELP to recieve instructions, enter QUIT to end, or enter START to begin.";
cin.ignore();
cout<<"Commands are case-sensitive, and you must press Enter after inputting your choice.\n";
cin>>answer;
if (answer == "HELP") {
cout<<"So, here's your help.\n";
cin.ignore();
cout<<"Questions will be asked of you, and you, well, have to answer them.";
cin.ignore();
cout<<"Should kind of be obvious.";
cin.ignore();
cout<<"However, should you happen not to be me, or someone who has never used the C++ language before,";
cin.ignore();
cout<<"you need to know a few things.";
cin.ignore();
cout<<"Answers must be in all caps, and you must press Enter after typing your answer.";
cin.ignore();
cout<<"If you don't know how to type, then, well, I don't know what to tell you.";
cin.ignore();
}
if (answer == "START") {
cout<<"Well, it's time to start the game!\n";
cin.ignore();
cin.ignore();
cout<<"3";
cin.ignore();
cout<<"2";
cin.ignore();
cout<<"1";
cin.ignore();
cout<<"GO!";
cin.ignore();
cout<<"First question!\n";
cout<<"What is 1 + 1?\n";
cout<<"A: 1\n";
cout<<"B: 2\n";
cout<<"C: 3\n";
cout<<"D: 4\n";
cin>>answer;
if (answer == "B") {
sr=sr+1;
}
if (answer != "B") {
sr=sr-1;
}
if (sr==1) {
cout<<"Second question!\n";
cout<<"What is the square root of 64?\n";
cout<<"A: 4\n";
cout<<"B: 9\n";
cout<<"C: 8\n";
cout<<"D: 6\n";
cin>>answer;
if (answer == "C") {
sr=sr+1;
}
}
if (sr==-1) {
cout<<"Second question!\n";
cout<<"What is the square root of 64?\n";
cout<<"A: 3\n";
cout<<"B: 9\n";
cout<<"C: 8\n";
cout<<"D: 6\n";
cin>>answer;
if (answer == "C") {
sr=sr+1;
}
};
if (answer == "QUIT") {
sa=0;
}
cin.get();
};
};
}
#包括
使用名称空间std;
int main(int argc,const char*argv[]{
int sa;
sa=2;
int-sr;
sr=0;
如果(sa!=0)
sa=1;
字符串回答;
而(sa==1){
如果(sa==1)
cout你的if(answer==“QUIT”)
在if(answer==“START”)
内if(answer==“QUIT”)
在if(answer==“START”)
内将if(answer==“QUIT”)
移出if(answer==“START”)
块
到目前为止,它看起来是这样的:
if (answer == "HELP") {
...
}
if (answer == "START") {
...
if (answer == "QUIT") {
...
}
}
而且它必须是
if (answer == "HELP") {
...
}
if (answer == "START") {
...
}
if (answer == "QUIT") {
...
}
将if(answer==“QUIT”)
移出if(answer==“START”)
块
到目前为止,它看起来是这样的:
if (answer == "HELP") {
...
}
if (answer == "START") {
...
if (answer == "QUIT") {
...
}
}
而且它必须是
if (answer == "HELP") {
...
}
if (answer == "START") {
...
}
if (answer == "QUIT") {
...
}
在所有其他答案被询问/回答后,您只需查找一次QUIT
。如果您希望用户能够在每个问题上完全退出,您必须在每个问题上进行检查,而不仅仅是在测试结束时。这只是一个有用的提示,当您尝试调试未来的程序时,请始终使用大括号{和}即使它们是可选的,例如只包含一条语句的“if”语句。这将帮助您直接获得缩进和大括号匹配。第二点:当向stackoverflow发布问题以寻求帮助时,最好只发布暴露问题所需的最低代码。我想,尤其是在这种情况下,您可能已经在练习过程中,您自己发现了问题。在所有其他答案被询问/回答后,您只需查找一次QUIT
。如果您希望用户能够在每个问题上完全退出,您必须在每个问题上进行检查,而不仅仅是在测试结束时。这只是您尝试调试您的应用程序时的一个有用提示在将来的程序中,始终使用大括号{和}即使它们是可选的,例如只包含一条语句的“if”语句。这将帮助您直接获得缩进和大括号匹配。第二点:当向stackoverflow发布问题以寻求帮助时,最好只发布暴露问题所需的最低代码。我想,尤其是在这种情况下,您可能已经在练习过程中自己发现了问题。