0

我遇到了这个应用程序的逻辑错误。这是一个单词混词应用程序,它显示一个混杂的单词,并询问玩家他/她是否想在猜对后再次播放。

当我告诉应用程序我不想再玩时,它无论如何都会继续播放序列。我有一种感觉,这对我来说嵌套不好。

#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", "Are you banging your head against something?"},
        {"jumble", "Its what this game is all about."},
        {"glasses", "You might need these to read this text."},
        {"labored", "Going slowly, is it?"},
        {"persistent", "Keep at it."},

    };

    srand(static_cast<unsigned int>(time(0)));

    cout << "\t\tWelcome to Word Jumble!\n\n";
    cout << "Unscramble the the letters to make the word!\n";
    cout << "Enter 'hint' for a hint\n";
    cout << "Enter 'quit' to quit the game\n\n";



    const int MAX_LEVEL = NUM_WORDS - 1;
    int totalScore = 0;

    for (int level = 0; level <= MAX_LEVEL; ++level)
    {
        string theWord = WORDS[level][WORD]; // Word to guess
        string theHint = WORDS[level][HINT]; // Word hint
        char playAgain;
        string jumble = theWord; //Jumbled version of the word
        int length = jumble.size();
        int score = jumble.size() * 10;

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

        cout << jumble << endl;

        string guess;
        cout << "\nYour Guess: ";
        cin >> guess;


        while ((guess != theWord) && (guess != "quit"))
        {
            if (guess == "hint")
            {
                cout << theHint;
                score = score / 2;
            }

            else
            {
                cout << "\n\nSorry thats not it.\n\n";
            }

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

        }

        if (guess == theWord)
        {
            cout << "Thats it! You guessed it!\tYou scored: " << score << "\n\n";

            cout << "Would you like to play again? (y/n): ";
            cin >> playAgain;

            if (playAgain = 'y')
            {
                continue;
            }

            else if (playAgain = 'n')
            {
                cout << "Your total score is: " << totalScore << endl;
                break;
            }

        }



        else if (guess == "quit")
        {
            if (totalScore > 0)
            {
                cout << "Your total score is: " << totalScore << endl;
            }

            break;
        }
    }

    cout << "\nGoodbye.";

    return 0;
}
4

2 回答 2

3

与and比较playAgain时,只有一个等号,导致第一个 ( ) 始终执行而不是实际选择,因为 的值不是 0。'y''n''y''y'

要解决此问题,它们应该是:

if (playAgain == 'y') //note ==
{
    continue;
}

else if (playAgain == 'n') //note ==
{
    cout << "Your total score is: " << totalScore << endl;
    break;
}

此外,如果您打开了警告,任何理智的(更现代的)编译器都应该警告您。一定要打开它们并注意它们。

于 2012-10-02T04:51:38.120 回答
1

我认为您将需要 == 来解决您的 playAgain 问题。我经常在这方面犯错。

于 2012-10-02T04:52:43.810 回答