【问题标题】:When I made a do/while loop in C++ in order to allow a player to play the game again, the game only loops once, how do I fix this?当我在 C++ 中创建一个 do/while 循环以允许玩家再次玩游戏时,游戏只循环一次,我该如何解决这个问题?
【发布时间】:2020-07-18 05:34:01
【问题描述】:
char playAgain;
do
{
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];   //word to guess
    int wrong = 0;                      //number of incorrect guesses
    string soFar(THE_WORD.size(), '-'); //word guessed so far
    string used = "";                   //letter alrdy guessed

    cout << "Welcome to Hangman. Good luck!\n";

    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\n\nYou have " << (MAX_WRONG - wrong);
        cout << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;

        char guess;
        cout << "\n\nEnter you guess: ";
        cin >> guess;
        guess = toupper(guess);  //make uppercase since secret word in uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "You've already guessed " << guess << endl;
            cout << "Enter you guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.\n";

            //update soFar to include newly guessed letter
            for (int i = 0; i < THE_WORD.length(); ++i)
            {
                if (THE_WORD[i] == guess)
                {
                    soFar[i] = guess;
                }
            }
        }
        else
        {
            cout << "Sorry, " << guess << " isn't in the word.\n";
            ++wrong;
        }
    }

    //shut down
    if (wrong == MAX_WRONG)
    {
        cout << "\nYou've been hanged!";
    }
    else
    {
        cout << "\nYou guessed it!";
    }

    cout << "\nThe word was " << THE_WORD << endl;
    cout << "\nPlay again? < Y , N >\n";
    cin >> playAgain;
} while ((playAgain == 'Y') || (playAgain == 'y'));
return 0;

您好,我已经尝试让这款游戏成为一款可重复的游戏。我成功了,但注意到当玩家选择 Y 或 y 时游戏只会循环一次。这显然是因为 playAgain 现在保留了该值。我该如何解决这个问题,以便 playAgain 在游戏再次开始时“忘记”用户输入的内容?感谢您的帮助。

【问题讨论】:

  • 不相关:srand(static_cast&lt;unsigned int&gt;(time(0))); 应该只执行 一次 - 将它移到循环之前,就在程序的开头。
  • 注意:random_shuffle 在 C++14 中被弃用,在 C++17 中被移除。请改用shuffle。另外,请发送minimal reproducible example。
  • playAgain = 'n'; 在循环的顶部?读取单个char 非常脆弱,如果存在额外的无关字符(例如输入"yes" 而不是'y'),则在每个输入之后考虑std::basic_istream::ignore。您的输入没有任何明显错误。
  • 感谢大家的cmets,大约一个月前我刚刚开始自己​​学习C++,这本书显然有点过时了。我可能会继续读完这本书以理解这些概念并继续学习现代在线教程。

标签: c++ loops while-loop


【解决方案1】:

我看到循环不起作用的唯一方法是如果缓冲区中有尚未读取的字符,请使用cin.ignore() 清除该行。您需要#include &lt;limits&gt; 才能使用:

    ...
    cout << "\nPlay again? < Y , N >\n";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    if(!(cin >> playAgain)) break; // if extraction fails, we're probably at EOF
) while(...);

【讨论】:

  • 我也会责骂验证,例如if (!(std::cin &gt;&gt; playAgain)) break;
  • @DavidC.Rankin 好主意,已添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 2013-10-05
  • 2015-01-16
  • 2018-10-05
相关资源
最近更新 更多