試圖創建一個簡單的基於文本的遊戲,一切都已經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;
}
如果用戶輸入「N」,那麼你的while循環的條件下仍然如此 - 所以它開始。 –
但我認爲它是在它自己的塊 –
我建議你格式化代碼 - 嵌套如果語句很難看,當他們沒有正確對齊。此外,你的老師或其他人正在爲此工作將感謝你 - 相信我;) –