0

それで、私は C++ のコーディング方法を独学で学んでいます。Michael Dawson 著『Beginning C++ through game programming』の第 3 版を購入しました。この章には、ループについて学ぶ章があります。章の終わりに向かって、単語をごちゃまぜにするゲームを作成します。私はそれをもう少し進めたかったので、ランダムに選ぶ5つの単語を作りました。ただし問題は、常に 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番目のケース以降、最終的に to の値を変更しpreWordます5

于 2013-05-23T01:40:50.607 に答える
1

preWord を 5 に設定していますif (preWord = 5)。同等性をチェックしていません。である必要がありますif (preWord == 5)。警告をオンにしてコンパイルすると、この問題が発生することはありません。

于 2013-05-23T01:41:46.887 に答える
0

=は割り当てであり、==比較です。すべてのifs を使用する必要があるので、次の==ように変更しますif (preWord == 4)

また、int preWord = rand() % 3; 0 から 2 の範囲の乱数を生成しますint preWord = rand() % 5 +1。1 から 5 の範囲の数値を生成するために使用する必要があります

于 2013-05-23T01:43:32.600 に答える