在使此while循环正常工作时遇到麻烦

时间:2019-02-21 02:39:09

标签: c++ loops while-loop

因此,我正在尝试创建一个小游戏,在其中我想到一种动物(在这种情况下),并提示用户猜测它是什么,甚至如果他们不断弄错它也会给他们一些提示。但是,该程序似乎只能运行一次,当我询问用户是否要重试时,它只会不断重复相同的消息。解决此问题的任何提示都会对我有很大帮助。

PS:缺席很长时间之后,我才刚开始回到C ++,所以我的技能有点生锈。

#include <iostream>
using namespace std;

int main()
{
    string animal = "fish";
    string guess;
    char choose = 'Y';
    int count = 0;
    int limit = 5;
    bool out_of_guesses = false;

    cout << "I am thinking of an animal.\n" << endl;

    do
    {
        while (animal != guess && !out_of_guesses)
        {
            if (count < limit)
            {
                cout << "Can you guess what animal I am thinking of?: ";
                getline(cin, guess);
                count++;
                if (animal != guess)
                {
                    cout << "\nHmm, nope. That's not the animal I'm thinking of." << endl;
                    if (count > 2 && count < 5)
                    {
                        cout << "I'll give you a hint. It lives in water." << endl;
                    }
                }
            }
            else
            {
                out_of_guesses = true;
            }
        }
        if (out_of_guesses)
        {
            cout << "\nI'm sorry, but you are out of guesses.\n" << endl;
        }
        else
        {
            cout << "\n*** Good job! You guessed the correct animal! ***\n" << endl;
            cout << "\t\t><)))º> ❤ <º)))><\t\t" << endl;
        }
        cout << "\nWould you like to try again?: ";
        cin >> choose;
    } while (choose == 'Y' || 'y');
    return 0;
}

0 个答案:

没有答案