而在Hangman程序C ++中循环

时间:2015-11-19 21:34:51

标签: c++ loops if-statement while-loop

 while (wrongGuess != 6)
    {
        cout << "\nEnter a letter to guess: ";
        cin >> wrongGuess;
        wrongGuess = toupper(wrongGuess);
        cout << "You guessed the letter: " << wrongGuess << endl;

        for (int i = 0; i < fileWord.length(); i++)
        {
            if (fileWord[i] == wrongGuess)
            {
                cout << wrongGuess << " is in the letter to guess." <<       endl;
            found = true;
        }

    }
    // if not found - increment wrong guesses
    if (!found)
    {
        wrongGuess++;
        cout << wrongGuess << " is not in the word to guess." << endl;
    }
    //print the board that corresponds to the wrongGuess

}

while循环的第一部分工作它将显示“在猜词中”。但是,当我输入一个不正确的字母时,它不会显示“不在猜词中”,而是显示“你猜错了”。我的逻辑出了什么问题?

3 个答案:

答案 0 :(得分:1)

您正在使用wrongGuess作为用户的输入以及错误猜测的计数器...... 另外,我不知道whatGuess和fileWord的类型是什么,但我猜你是在比较一个int和一个可能没有给出预期结果的char。

答案 1 :(得分:0)

我根本没有测试过这段代码,但是它应该让你为正确设计程序做好准备。

conda remove qt
conda remove pyqt

答案 2 :(得分:0)

我认为你需要另一个变量,你使用wrongGuess来控制while并从用户那里读取输入。

我不知道,但试试这个。

int count = 0;
while (count != 6)
{
    cout << "\nEnter a letter to guess: ";
    cin >> wrongGuess;
    wrongGuess = toupper(wrongGuess);
    cout << "You guessed the letter: " << wrongGuess << endl;

    for (int i = 0; i < fileWord.length(); i++)
    {
        if (fileWord[i] == wrongGuess)
        {
            cout << wrongGuess << " is in the letter to guess." << endl;
            found = true;
        }

   }
// if not found - increment wrong guesses
if (!found)
{
    count++;
    cout << wrongGuess << " is not in the word to guess." << endl;
}
//print the board that corresponds to the wrongGuess
}
相关问题