逻辑错误,需要帮助

时间:2012-10-02 04:45:04

标签: c++

我在使用此应用时遇到了逻辑错误。这是一个单词混乱的应用程序,显示一个混乱的单词,并询问玩家他/她是否想要一旦他们正确猜测再次播放。

当我告诉应用程序我不想再玩时,无论如何都会继续执行。我有一种感觉,就是我的不良筑巢。

#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;
}

2 个答案:

答案 0 :(得分:3)

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

要解决这个问题,他们应该是:

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

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

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

答案 1 :(得分:1)

我认为你需要==为你的playAgain提问。我经常犯错误。