2014-04-14 47 views
0

我試圖給我的程序添加一個「退出」功能,但輸入不起作用。進入「退出」後,程序將正常重啓。我該如何改變這一點?我試過固定括號等,但迄今爲止,絕對沒有任何工作。退出功能不起作用

#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

不斷尋找'QUIT'已被詢問/回答。如果您希望用戶能夠在每個問題上完全退出,那麼您必須在每個問題上檢查它,而不僅僅是在測試結束時進行檢查。 –

+1

只要您嘗試調試將來的程序時提供有用的提示,即使它們是可選的,也應始終使用花括號{和},例如僅包含一條附帶語句的'if'語句。這將幫助您獲得縮進和大致匹配。第二點:將問題發佈到stackoverflow尋求幫助時,最好只發布暴露該問題所需的最低代碼。我認爲,特別是在這種情況下,你可能在練習過程中自己發現了問題。 – alpartis

回答

1

if (answer == "QUIT")裏面if (answer == "START")

3

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

截至目前,它看起來像這樣:

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

和它需要你只** ** ONCE所有其他的答案後

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