尝试创建一个简单的基于文本的游戏,一切都好了,但是当用户将N输入到userinput2时,它不仅会打印文本而且还会把我带回菜单我不知道为什么有人可以解释这个我吗?
#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;
}
答案 0 :(得分: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
的计算结果为false,换句话说,直到done
设置为true。在此循环中,您将打印出起始屏幕。因此,在将完成设置为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
}
因为当用户输入done
时,您永远不会更改'N'
的值,循环会继续循环。如前所述,每次循环运行时都会打印开始屏幕。
要解决此问题,您需要在游戏结束时设置done
的值。通过使支撑和缩进更清晰,可以更容易地在视觉上发现这些更改发生的位置,这是我和其他人建议您改进代码格式的主要原因。
有很多不同的东西可以改进这个代码,但更多的是关于code review site的主题。
答案 1 :(得分:0)
只需查看您的代码,我就会检查每个{对比匹配},以确保程序按照您的预期流动。对我来说这些东西看起来不对。我只能看到4个紧密的括号,这些都在代码的末尾。
更好地格式化代码后,您可以更轻松地检查用户输入是否再次使用tolower(answer)
对应值为'N'
的变量。我认为tolower(answer)
永远不会等于大写字母。 - 回顾一下你的代码我不认为这实际上有什么作用,因为tolower()
函数是自己调用的。
答案 2 :(得分: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;
}
}
}