2012-02-23 79 views
1

我遇到了我的代碼問題。我正在嘗試構建一個遊戲,並且在到達主循環時出現錯誤。我將顯示代碼和我得到的錯誤。while和switch loops的問題

只需做一個小記錄,選擇選項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 

回答

1

我認爲該消息

redeclaration of `int choice'

應該是很明顯的。

其他錯誤信息有點難以理解,但是如果在switch語句之外聲明變量guess,它將消失。

+0

但是,爲什麼這是真的? – DutchLearner 2012-02-23 16:13:44

+1

@ user1222107我不知道技術細節,但它與標籤不會創建新塊有關。解決方法是聲明'switch'語句之外的所有變量,或者將它們放在一個'{}'塊中。你可以做'case X:{/ * stuff * /}'這樣的東西,並且在裏面聲明變量。 – 2012-02-23 16:46:58

+0

好的,謝謝。 :) – DutchLearner 2012-02-23 16:54:49

2

你聲明int choice兩次。這個錯誤信息很清楚。

一旦你聲明的變量,你不能在同一範圍內重新聲明它:

{ 
    int x; 

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

您已經聲明choice在兩個地方。此外,您已在while循環之外編寫了switch,這意味着即使您的程序編譯,它也會陷入無限循環。

+0

@JoachimPileborg你看到'choiceNotMade'越來越設爲'FALSE'在'while'? – 2012-02-23 14:49:27

+0

不要緊,因爲在'之開關替代3調用'exit'和'choiceNotMade'設置爲'FALSE'在替代2. – 2012-02-23 14:50:49

+0

@JoachimPileborg我談論外面的'而寫的switch語句( choiceNotMade)''這個循環肯定會導致無限循環。通過查看縮進,看起來好像是要在該循環內寫入開關。 – 2012-02-23 14:50:52

1

我認爲這是一個家庭作業,所以我會遠離過多的具體建議了。

您宣佈第19行的變量choice,所以你需要在第34

您還需要switch語句之前的string guess聲明移到刪除第二個聲明。這是因爲C++要求所有當地人精確一次初始化,並且沒有辦法編譯器可以確保的是,如果在第一時間通過你拿case 2:循環和第二次,它需要case 1:其中string guess聲明。

這將使您的程序編譯,但它不會按預期工作。查看程序中的開啓/關閉大括號,並確保代碼塊以您期望的方式嵌套。

1

您聲明

int choice; 

兩次,一次

int choice = (rand() % NUM_WORDS); 

,然後,對前

while (choiceNotMade)