while和switch循环的问题

时间:2012-02-23 14:39:53

标签: c++ while-loop switch-statement

我的代码存在问题。我正在尝试构建一个游戏,它在到达主循环时会以某种方式出错。我将展示代码和我得到的错误。

只是一个小小的注释,选择选项1并玩游戏的关键是游戏在给出正确答案后循环并向玩家提供第二个随机单词,并继续这样做直到玩家写'退出”。

这是代码:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 3;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"jumble1", "First word."},
        {"jumble2", "Second word."},
        {"jumble3", "Third word."}
    };

    srand(static_cast<unsigned int>(time(0)));
    int choice = (rand() % NUM_WORDS);
    string theWord = WORDS[choice][WORD];
    string theHint = WORDS[choice][HINT];

    string jumble = theWord;
    int length = jumble.size();
    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;
    }

    int choice;
    bool choiceNotMade = true;

    while (choiceNotMade)
    {
        cout << "[1] Play\n";
        cout << "[2] Credits\n";
        cout << "[3] Quit\n\n";

                cout << "Your choice: ";
        cin >> choice;
        }

            switch (choice)
            {
            case 1:
                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 jumble is: " << jumble;

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

                while ((guess != theWord) && (guess != "quit"))
                {
                    if (guess == "hint")
                    {
                        cout << theHint;
                    }
                    else
                    {
                        cout << "That's not the right word.";
                    }

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

                if (guess == theWord)
                {
                    cout << "\nYou guessed it!\n";
                }

                cout << "\nThank you for playing.\n";

                system("Pause");
                choiceNotMade = false;
                break;

            case 2:
                cout << "\n\nThis game has been made by:\n\n";
                choiceNotMade = false;
                break;

            case 3:
                cout << "Program will exit";
                exit(1);

            default:
                cout << "\nYou did not pick a valid option.\n\n";
                choiceNotMade = false;
                break;  

                } 

    return 0;
}

这就是错误:

word_jumble.cpp: In function `int main()':
word_jumble.cpp:32: error: redeclaration of `int choice'
word_jumble.cpp:17: error: `int choice' previously declared here
word_jumble.cpp:83: error: jump to case label
word_jumble.cpp:53: error:   crosses initialization of `std::string guess'
word_jumble.cpp:88: error: jump to case label
word_jumble.cpp:53: error:   crosses initialization of `std::string guess'
word_jumble.cpp:92: error: jump to case label
word_jumble.cpp:53: error:   crosses initialization of `std::string guess'
word_jumble.cpp:83: warning: destructor needed for `guess'
word_jumble.cpp:83: warning: where case label appears here
word_jumble.cpp:83: warning: (enclose actions of previous case statements requiring destructors in their own scope.)
word_jumble.cpp:88: warning: destructor needed for `guess'
word_jumble.cpp:88: warning: where case label appears here
word_jumble.cpp:92: warning: destructor needed for `guess'
word_jumble.cpp:92: warning: where case label appears here
word_jumble.cpp:100:2: warning: no newline at end of file
make[2]: *** [build/Debug/MinGW-Windows/word_jumble.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

5 个答案:

答案 0 :(得分:2)

您宣布int choice两次。错误信息很清楚。

一旦声明了变量,就无法在同一范围内重新声明它:

{
  int x;

  //...
  int x; // <-- illegal, just use x
}

答案 1 :(得分:1)

您已在两个地方宣布choice。此外,您已在switch循环之外编写while,这意味着即使您的程序编译,它也会陷入无限循环。

答案 2 :(得分:1)

我认为这是一个家庭作业,所以我会远离太具体的建议。

您在第19行声明了变量choice,因此您需要在第34行删除第二个声明。

您还需要将string guess的声明移到switch声明之前。这是因为C ++要求所有本地文件只需要初始化一次,如果第一次通过循环,case 2:并且第二次需要case 1:,编译器就无法确保这一点。 }声明string guess的地方。

这将使您的程序编译,但它不会按预期工作。查看程序中的开始/结束括号,并确保代码块以您期望的方式嵌套。

答案 3 :(得分:1)

我认为消息

  

重新声明`int choice'

应该很明显。

其他错误消息有点难以理解,但如果在guess语句之外声明变量switch,则会消失。

答案 4 :(得分:1)

你宣布

int choice; 

两次,一次

int choice = (rand() % NUM_WORDS);

然后,就在

之前
while (choiceNotMade)