循环增量不起作用

时间:2016-06-15 17:08:25

标签: c++ loops iteration

我是编程的初学者,也是论坛的新手。我一直在做一个开始项目,一个Hangman游戏。我正在使用迭代常量numberGuesses来跟踪用户输入了多少猜测。但是,我的程序没有正确迭代,无论出于什么原因我都无法理解。非常感谢任何帮助!

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string userWord = " ";
    char userLetter = ' ';
    int numberGuesses = 0;
    string board1 =
        " -------|\n"
        " |      |\n"
        "        |\n"
        "        |\n"
        "        |\n"
        "        |\n"
        "      -----";
    string board2 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        "        |\n"
        "        |\n"
        "        |\n"
        "      -----";
    string board3 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        " |      |\n"
        "        |\n"
        "        |\n"
        "      -----";
    string board4 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        "-|      |\n"
        "        |\n"
        "        |\n"
        "      -----";
    string board5 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        "-|-     |\n"
        "        |\n"
        "        |\n"
        "      -----";
    string board6 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        "-|-     |\n"
        "/       |\n"
        "        |\n"
        "      -----";
    string board7 = 
        " -------|\n"
        " |      |\n"
        " O      |\n"
        "-|-     |\n"
        "/ \\     |\n"
        "        |\n"
        "      -----";

    cout << "Enter a word to guess: ";
    cin >> userWord;
    for (int i = 0; i < userWord.length(); i++)
       {
           userWord[i] = toupper(userWord[i]);
       }
    cout << "You entered: " << userWord << endl;



    do 
    {

        if (numberGuesses = 0)
        {
            cout << board1;
        }    
        if (numberGuesses = 1)
        {
            cout << board2;
        }
        else if (numberGuesses = 2)
        {
            cout << board3;   
        }
        else if (numberGuesses = 3)
        {
            cout << board4;
        }
        else if (numberGuesses = 4)
        {
            cout << board5;
        }
        else if (numberGuesses = 5)
        {
            cout << board6;
        }
        else if (numberGuesses = 6)
        {
            cout << board7;
        }

        cout << "\nEnter a letter to guess: ";
        cin >> userLetter;
        userLetter = toupper(userLetter);
        cout << "You entered: " << userLetter << endl;

        if (userWord.find(userLetter) != string::npos)
            cout << userLetter << " is in the word to guess." << endl;
        else
            cout << userLetter << " is NOT in the word to guess." << endl;

        numberGuesses++;

    }

    while (numberGuesses <= 6);

    return 0;
}

1 个答案:

答案 0 :(得分:10)

=是赋值运算符。要检查是否相等,您应该使用==运算符:

if (numberGuesses == 0) {
    // Here -------^
    // (and do the same for the other conditions)