【问题标题】:Looping Increment Not Working循环增量不起作用
【发布时间】:2016-06-15 17:08:24
【问题描述】:

我是编程初学者,也是论坛的新手。我一直在做一个开始的项目,一个刽子手游戏。我正在使用迭代常数 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;
}

【问题讨论】:

  • 您可以通过使用boards(或字符串)数组来消除大量if 语句。类似cout &lt;&lt; board_array[numberGuesses] &lt;&lt; endl;

标签: c++ loops iteration


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 2014-09-02
    • 2012-05-17
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多