2015-06-24 65 views
0

試圖創建一個簡單的基於文本的遊戲,一切都已經OK了,但fr當用戶輸入N到userinput2它不僅打印文本,而且還帶我回到菜單我有不知道爲什麼有人能向我解釋這一點?C++基於文本的遊戲(未知錯誤)

#include <iostream> 
#include <cmath> 
#include <string> 
using namespace std; 

int main(){ 
    bool done = false; 
     while (!done) { 
     char userinput; 
     string name; 
     char yes = 'Y'; 
     char no = 'N'; 
     char userinput2; 


    cout << "#############################################" << endl; 
    cout << "#=======|| ## The Age OF Zorak ## ||=======#" << endl; 
    cout << "#############################################" << endl; 
    cout << " ####################################### " << endl; 
    cout << "  #################################  " << endl; 
    cout << "  #############################   " << endl; 
    cout << "   #########################   " << endl; 
    cout << "   ======================   " << endl; 
    cout << "    ==================    " << endl; 
    cout << "    ==============    " << endl; 
    cout << "     {}{}{}{}{}     " << endl; 
    cout << "     ........     " << endl; 
    cout << "     ||||||     " << endl; 
    cout << "     ||||      " << endl; 
    cout << "      ||      " << endl; 
    cout << "     ()      " << endl; 
    cout << "            " << endl; 


    cout << "\n"; 
    cout << "\n"; 
    cout << "\n"; 
    cout << "\n"; 

    cout << "---------------------------------------------" << endl; 
    cout << "    ----Start----     " << endl; 
    cout << "---------------------------------------------" << endl; 
    cout << "    ---- Y/N ----     " << endl; 
    cout << "---------------------------------------------" << endl; 
    cout << ">>: ";   
    cin >> userinput; 
    if (userinput == yes){ 
     cout <<"Welcome,stranger what is your name?" << endl; 
     cout <<">>: "; 
     cin >> name; 
     cout << "I see a long road for you " <<name <<"," << endl; 
     cout << "Perhaps you would like some water?" << endl; 
     cout << ">>Take the water from the old man?<<" << endl; 
     cout << "(Y/N)>>: "; 
     cin >> userinput2; 

      if (userinput2 == 'N'){ 
      cout << "You refuse the water but thank the man for the offer." << endl; 
      cout << "leaving the Inn, you feel much rested but your coin purse" <<endl; 
      cout << "feels light...It's time to get some gold!!!!" << endl;  
      } 

       if (userinput2 == yes){ 
       cout << "You sip the water and thank the kind old man." << endl; 
       cout << "Moments after drinking the water,the room begins to spin"<< endl; 
       cout << "the old man's laughter is the last thing you hear...." << endl; 
       cout << "<<< You are DEAD >>>" << endl; 
       cout << "<<< Try again? >>>" << endl; 
       cout << "(Y/N)" << endl; 

       char answer; 
       cin >> answer; 
       tolower(answer); 
       if (answer == no) done = true; 
     } 
     }   

    } 

    return 0; 
} 
+2

如果用戶輸入「N」,那麼你的while循環的條件下仍然如此 - 所以它開始。 –

+0

但我認爲它是在它自己的塊 –

+4

我建議你格式化代碼 - 嵌套如果語句很難看,當他們沒有正確對齊。此外,你的老師或其他人正在爲此工作將感謝你 - 相信我;) –

回答

2

在這一切的開始你有一個while循環

bool done = false; 
while (!done) { 
    //everything in here keeps looping until 
    //the condition doesn't hold at the beginning of this loop 
} 

這將繼續循環,直到,直到done設置爲true的條件!done計算爲false,或者換句話說。在此循環中,您將打印出開始屏幕。因此,直到您將完成設置爲true,此開始屏幕將繼續打印出來。

到目前爲止這麼好。

現在程序的行爲,正如預期的,因爲:

cout << "(Y/N)>>: "; 
    cin >> userinput2; 

     if (userinput2 == 'N'){ 
      //code for the no option 
      //note that done is never changed in here (important!) 
     } 

      if (userinput2 == yes){ 
      //code for yes option 
      char answer; 
      cin >> answer; 
      tolower(answer); 
      if (answer == no) done = true; //done IS handled but only in the "yes" branch from above 
    } 

當用戶進入'N'繼續循環,循環上因爲你永遠不會改變的done值。如前所述,每次循環運行時都會打印開始屏幕。

要解決您需要在遊戲結束時設置值done。通過使括號和縮進更加清晰,可以更直觀地發現這些更改發生的位置,這是我和其他人建議您改進代碼格式的主要原因。

關於此代碼有很多不同的事情可以改進,但是更多關於code review site的主題。

+0

謝謝你花時間向我解釋這個!我會盡量改進我的縮進,因爲你建議 –

0

看看你的代碼,我會檢查每個{與匹配的},以確保程序按照您的預期流動。有些東西對我來說看起來不正確。我只能看到4個大括號,這些都在你的代碼結尾。

一旦您更好地對代碼進行了格式化,可以更容易地看到您正在檢查用戶輸入,以tolower(answer)再次對'N'的變量進行更改。我認爲tolower(answer)永遠不會等於大寫char。 - 回頭看看你的代碼,我認爲這實際上不會做任何事情,因爲它自己調用tolower()函數。

+0

檢查大括號,並使它們更有序...但錯誤仍然發生在改變代碼塊之外的while循環條件,應該做任何想法? –

+0

正如我理解你的代碼,你打算'if'語句來執行一些條件操作,但沒有開放的'{'和關閉'}'大括號正確結構,所有的代碼運行。你在你的例子中選擇拒絕水,但如果你接受,我猜你會看到相同的文字'你拒絕水,但是謝謝......'加上預期的文字。這是否給你一些想法在哪裏看? – Swinders

+0

http://i.imgur.com/PIhAQww.png –

0

這裏的人,開始與此:

bool done = false; 
    while (!done) { 
     char userinput; 
     string name; 
     char yes = 'Y'; 
     char no = 'N'; 
     char userinput2; 


     cout << "#############################################" << endl; 
     cout << "#=======|| ## The Age OF Zorak ## ||=======#" << endl; 
     cout << "#############################################" << endl; 
     cout << " ####################################### " << endl; 
     cout << "  #################################  " << endl; 
     cout << "  #############################   " << endl; 
     cout << "   #########################   " << endl; 
     cout << "   ======================   " << endl; 
     cout << "    ==================    " << endl; 
     cout << "    ==============    " << endl; 
     cout << "     {}{}{}{}{}     " << endl; 
     cout << "     ........     " << endl; 
     cout << "     ||||||     " << endl; 
     cout << "     ||||      " << endl; 
     cout << "      ||      " << endl; 
     cout << "     ()      " << endl; 
     cout << "            " << endl; 


     cout << "\n"; 
     cout << "\n"; 
     cout << "\n"; 
     cout << "\n"; 

     cout << "---------------------------------------------" << endl; 
     cout << "    ----Start----     " << endl; 
     cout << "---------------------------------------------" << endl; 
     cout << "    ---- Y/N ----     " << endl; 
     cout << "---------------------------------------------" << endl; 
     cout << ">>: ";   
     cin >> userinput; 
     if (userinput == 'Y'){ 
      cout <<"Welcome,stranger what is your name?" << endl; 
      cout <<">>: "; 
      cin >> name; 
      cout << "I see a long road for you " <<name <<"," << endl; 
      cout << "Perhaps you would like some water?" << endl; 
      cout << ">>Take the water from the old man?<<" << endl; 
      cout << "(Y/N)>>: "; 
      cin >> userinput2; 

      if (userinput2 == 'N'){ 
       cout << "You refuse the water but thank the man for the offer." << endl; 
       cout << "leaving the Inn, you feel much rested but your coin purse" <<endl; 
       cout << "feels light...It's time to get some gold!!!!" << endl;  
      } 

      if (userinput2 == 'Y'){ 
       cout << "You sip the water and thank the kind old man." << endl; 
       cout << "Moments after drinking the water,the room begins to spin"<< endl; 
       cout << "the old man's laughter is the last thing you hear...." << endl; 
       cout << "<<< You are DEAD >>>" << endl; 
       cout << "<<< Try again? >>>" << endl; 
       cout << "(Y/N)" << endl; 

       char answer; 
       cin >> answer; 
       tolower(answer); 
       if (answer == 'n') done = true; 
      } 
     }   

    } 
+0

謝謝,但錯誤仍然存​​在! –

+2

對,我沒有爲你修復這個錯誤 - 我認爲你理解它爲什麼不起作用是很重要的。我只需要格式化就不會讓我的頭受傷。 –

+0

當您在userinput2中鍵入'N'時,完成的變量仍然設置爲false。所以,循環再次從頂部開始。這基本上啓動你的遊戲。 –