gpt4 book ai didi

基于 C++ 文本的游戏(未知错误)

转载 作者:行者123 更新时间:2023-11-28 02:25:05 26 4
gpt4 key购买 nike

尝试创建一个简单的基于文本的游戏并且一切顺利所以 fr 但是当用户在 userinput2 中输入 N 它不仅打印文本而且还带我回到菜单我不知道为什么有人可以解释这个我?

#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;
}

最佳答案

在这一切的开始你有一个 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。在这个循环中,您打印出起始屏幕。因此,在您将 done 设置为 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 的主题。 .

关于基于 C++ 文本的游戏(未知错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31029841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com