2012-02-23 12 views
1

私のコードに問題があります。私はゲームを構築しようとしているし、メインループに到達すると何とかエラーが出る。私はコードと私が得ているエラーに表示されます。whileループとスイッチループに関する問題

オプション1を選択してゲームをプレイするときのポイントは、正しい答えが与えられた後にゲームがループし、2番目のランダムな単語をプレイヤーに提示し、プレイヤーが '終了する'。

これはコードです:

#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ステートメントの外に宣言すると消えます。

+0

しかし、なぜこれは実際にですか? – DutchLearner

+1

@ user1222107私は技術的な詳細はわかりませんが、そのケースのラベルは新しいブロックを作成しません。解決法は、 'switch'文の外で全ての変数を宣言するか、' {} 'ブロックに入れます。 'case X:{/ * stuff * /}'のようなやり方で変数を宣言することができます。 –

+0

申し訳ありません、ありがとうございます。 :) – DutchLearner

2

あなたが二回int choiceを宣言しています。エラーメッセージはそれでかなり明確です。

変数を宣言したら、あなたは同じスコープでそれを再宣言することはできません。

{ 
    int x; 

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

次の2つの場所でchoiceを宣言しました。また、whileループの外側にswitchと書いてあります。つまり、プログラムがコンパイルされても、無限ループに陥ることになります。

+0

@JoachimPileborgあなたはchoiceNotMade'の中では 'false'に設定取得'見ています「while」? –

+0

'switch'の代わりの3は' exit'を呼び出し、 'choiceNotMade'は選択肢2では' false'に設定されているので問題はありません。 –

+0

@JoachimPileborg私はswitch文が 'while choiceNotMade) 'ループは無限ループにつながるでしょう。字下げを見ると、そのループ内にスイッチを書き込む意図があるかのように見えます。 –

1

私はこれが宿題であると想定していますので、あまりにも具体的な推奨事項から離れていきます。

あなたはライン19上の変数choiceを宣言したので、あなたは、ライン34

で2番目の宣言を削除する必要がありますまた、switch文の前にstring guessの宣言を移動する必要があります。これは、C++では、すべてのローカルを正確に1回初期化する必要があるため、ループを初めて初めて実行する場合はcase 2:、それ以外の場合はcase 1:を返します。string guessが宣言されています。

これにより、プログラムがコンパイルされますが、期待どおりに動作しません。プログラムの中かっこを見て、コードブロックが期待どおりに入れ子になっていることを確認してください。

1

あなたは右の前に、その後、1回、2回

int choice = (rand() % NUM_WORDS); 

int choice; 

を宣言している

while (choiceNotMade)