c++ - 退出功能不起作用

标签 c++

我试图在我的程序中添加一个“QUIT”功能,但输入它不起作用。输入“QUIT”后,程序会正常重启。我怎样才能改变这个?我试过修复括号等,但到目前为止,完全没有任何效果。

#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<<"My 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();
        };
    };
}

最佳答案

if (answer == "QUIT") 移出 if (answer == "START") block 。

截至目前,它看起来像这样:

if (answer == "HELP") {
    ...
}
if (answer == "START") {
    ...
    if (answer == "QUIT") {
        ...
    }
}

这是必须的

if (answer == "HELP") {
    ...
}
if (answer == "START") {
    ...
}
if (answer == "QUIT") {
    ...
}

关于c++ - 退出功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067784/

相关文章:

c++ - 对象生命周期和未定义的行为

c++ - 'std' 以外的 C++ 中有用的命名空间

c++ - 如何将无限长度的二进制转换为十进制

c++ - 可选的命令行参数

c++ - 令人困惑的 gprof 输出

c++ - std::stringstream - 丢弃一个值

c++ - C中结构内的数组

c++ - 通过 boost::asio 发送原始数据

c++ - 模板对象的模板友元函数和命名空间

c++ - 使用 glDrawElementsInstanced 进行实例渲染时行为不一致,有时没有渲染也没有错误