2012-10-02 127 views
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; 
} 
+0

另外:你可以使用'std :: random_shuffle'來拼寫字母,而不是自己做。 – chris

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

而且,任何理智的(更現代化)編譯器應該提醒你注意這一點,如果你有警告開啓。一定要打開它們,並注意它們。

+0

是的。一個等號是賦值運算符,爲playAgain賦值「y」而不是創建一個參數。我應該看到這個,但沒有出於某種原因。謝謝! – Jammin

+0

@Jammin,當你意外地將它賦值爲0時,它總是更好,導致條件成爲假而永不執行,而不是始終執行。然後,你會坐在那裏撓撓頭,想知道爲什麼它似乎沒有擊中'continue'或'break'。 – chris

1

我想你會需要==爲你的playAgain問題。我經常犯這個錯誤。