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

C++ Text based Game ( Unknown Error )

尝试创建一个简单的基于文本的游戏并且一切顺利所以 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;
}

只要查看您的代码,我就会检查每个{是否匹配},以确保程序按您预期的方式运行。在我看来,这些东西有些不对劲。我只能看到 4 个右括号,这些都在你代码的末尾。

一旦您更好地格式化了代码,就更容易看到您正在检查用户输入以再次使用 tolower(answer) 与值 'N' 的变量进行比较。我认为 tolower(answer) 永远不会等于大写字符。 - 回顾你的代码,我认为这实际上没有做任何事情,因为 tolower() 函数是自己调用的。

在这一切的开始你有一个 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 上的主题。

伙计,从这个开始:

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

    }