退出功能不起作用

时间:2014-04-14 18:43:51

标签: 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<<"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();
        };
    };
}

2 个答案:

答案 0 :(得分:3)

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

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

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

它必须是

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

答案 1 :(得分:1)

if (answer == "QUIT")位于if (answer == "START")内。