0

所以我一直在自学如何编写 C++ 代码。我购买了Michael Dawson所著的《通过游戏编程开始 C++》,第三版,其中有一章介绍了循环。在本章快结束时,您制作了一个单词混乱的游戏。我想更进一步,并随机选择了 5 个单词。但问题是,它的值总是 5,因此总是选择第五个单词。

代码如下

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 5;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"wall", "Do you feel you're banging your head against something?"},
        {"glasses", "These might help you see the answer"},
        {"labored", "Going slowly is it?"},
        {"persistent", "Keep at it"},
        {"jumble", "It's what this game is about!"}
    };

    srand(time(0));
    int preWord = rand() % 3;
    string tempWord;
    string theWord = WORDS[preWord][WORD]; //word to guess
    string theHint = WORDS[preWord][HINT]; //hint for word
    if (preWord = 1)
    {
        theWord = "wall";
        tempWord = "wall";
    }
    if (preWord = 2)
    {
        theWord = "glasses";
        tempWord = "glasses";
    }
    if (preWord = 3)
    {
        theWord = "labored";
        tempWord = "labored";
    }
    if (preWord = 4)
    {
        theWord = "persistent";
        tempWord = "persistent";
    }
    if (preWord = 5)
    {
        theWord = "jumble";
        tempWord = "jumble";
    }

    int length = theWord.size();
    for (int i = 0; i < length; ++i)
    {
            int index1 = (rand() % length);
            int index2 = (rand() % length);
            char temp = theWord[index1];
            theWord[index1] = theWord[index2];
            theWord[index2] = temp;
    }

    cout << "\t\t\tWelcome to the Word Jumble!\n\n";
    cout << "Unscramble the letters to make a word.\n";
    cout << "Enter 'hint' for a hint.\n";
    cout << "Enter 'quit' to quit the game.\n\n";
    cout << "The word is: " << theWord;
    cout << "\nvariable 'preWord' is: " << preWord;
    string guess;
    cout << "\n\nYour guess: ";
    cin >> guess;

    while ((guess != tempWord) && (guess != "quit"))
    {
        if (guess == "hint")
        {
            cout << theHint;
        }
        else
        {
            cout << "Sorry, that's not it.";
        }

        cout <<"\n\nYour guess: ";
        cin >> guess;
    }

    if (guess == tempWord)
    {
        cout << "\nThat's it! You guessed it!\n";
    }

    cout << "\nThanks for playing!\n";

    return 0;
}
4

4 回答 4

5

在你的条件下,你应该写:

if (preWord == x)

这是一个比较,而不是

if (preWord = 5)

这是一个赋值- 这意味着,当您的代码到达这些if语句中的每一个时,它实际上会将值分配给您的变量 - 并且由于您的第 5 种情况是最后一种情况,它最终将更改preWordto的值5

于 2013-05-23T01:40:50.607 回答
1

您将 preWord 设置为 5。if (preWord = 5)不检查其相等性。应该是if (preWord == 5)。编译时出现警告,你永远不会遇到这个问题。

于 2013-05-23T01:41:46.887 回答
1

正如其他人指出的那样,您=在语句中使用了赋值运算符()if而不是相等运算符(==),因此您正在设置preWordat echif语句的值:

if (preWord = 5)
            ^

应该:

if (preWord == 5)
            ^^

我养成的一个习惯是将文字放在等式的另一边,如下所示:

if ( 5 == preWord )

如果您=改用它,那将是一个彻底的错误,gcc您会看到以下消息:

error: lvalue required as left operand of assignment

当然,打开警告总是很重要的,即使您在代码中使用了该表单,您也会看到以下警告gcc

warning: suggest parentheses around assignment used as truth value
于 2013-05-23T01:49:12.590 回答
0

=是赋值,==是比较。所以你所有的ifs都需要使用==......所以将它们更改为if (preWord == 4)

还有,int preWord = rand() % 3; 将生成 0 到 2 范围内的随机数。您需要int preWord = rand() % 5 +1用于生成 1 到 5 范围内的数字

于 2013-05-23T01:43:32.600 回答