2014-07-20 93 views
0

給予我的這個任務是從我們的教科書中複製一個Hang子手遊戲,並將其修改爲本書中的具體說明。我花了很多時間試圖研究這個問題,並找到一個原因讓我不斷收到我所做的同樣的錯誤信息。到處都是我看過的,每個試圖修改這個代碼的人都嘗試過使用數組,並且擁有和我一樣的幸運。我目前在Strings中做了一個章節,並計劃按照String中的說明請求我的大部分語句。如何跟蹤Hangman遊戲中C++已經猜到的字母?

我需要在此代碼修改是:

  1. 保持跟蹤輸入用戶
  2. 所有字母的錯誤信息發送給用戶,如果他們輸入一個字母是已經進入

我遇到的主要問題是,當我編譯的代碼,並比我早已經把在同一封信中,它與消息終止:

拋「的std ::超出範圍」 一個實例是什麼()終止後,被稱爲:basic_string的:: SUBSTR

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

    int main() 
    { 

     //declare variables 
     string origWord = " "; 
     string letter = " "; 
     char dashReplaced = 'N'; 
     char gameOver = 'N'; 
     int numIncorrect = 10; 
     string displayWord = "-----"; 
     string usedLetter = " "; 
     string letterNotGuessedYet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     int letterPlace = 0; 

     //get original word 
     do //begin loop 
     { 
      cout << "Enter a 5-letter word in uppercase: "; 
      getline(cin,origWord); 

     }while(origWord.length() != 5); //end do loop 

     //clear the screen 
     system("cls"); 

     //start guessing 
     cout <<"Guess this word: " << displayWord << endl; 

     while(gameOver =='N') 
     { 
      cout << "Enter an uppercase letter: "; 
      cin >> letter; 

      //This provides the letterPlace value with the possition of the 
      //input letter by the user within letterNotGuessedYet 
      letterPlace = letterNotGuessedYet.find(letter,0); 

      //This statement determines if the letter input by the user is 
      //still in the letterNotGuessYet string. 
      if(letterNotGuessedYet.substr(letterPlace,1)== letter) 
      { 
       letterNotGuessedYet.replace(letterPlace, 1, "*"); 
       //cout << endl << letterNotGuessedYet;   //This tests that a letter is being replaced with an * 
      }     
      else 
      { 
       cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl;   
      }// end if 


      //search for the letter in the original word 
      for(int x = 0; x < 5; x++) 
      { 
       //if the current character matches 
       //the letter, replace the corresponding 
       //dash in the displayWOrd variable and then 
       //set the dashReplaced variable to 'Y' 
       if (origWord.substr(x,1) == letter) 
       { 
        displayWord.replace(x, 1, letter); 
        dashReplaced = 'Y'; 
       }//end if 
      }//end for 

      //if a dash was replaced, check whether the 
      //displayWord variable contains any dashes 

      if (dashReplaced == 'Y') 
      { 
       //if the displayWord variable does not 
       //contain any dashes, the game is over 
       if (displayWord.find("-",0) == -1) 
       { 
        gameOver = 'Y'; 
        cout << endl << "Yes, the word is " << origWord << endl; 
        cout << "Great guessing!" << endl; 
       } 

       else //otherwise, continue guessing 
       { 
        cout << endl<< "Guess this word: " << displayWord << endl; 
        dashReplaced = 'N'; 
       }//end if 

      } 
      else //processed when dashReplaced contains 'N' 
      { 
       //minus 1 to the number of incorrect gueses left 
       numIncorrect += 1; 

       //if the number of incorrect guesses is 10, 
       //the game is over 
       if (numIncorrect == 10) 
       { 
        gameOver = 'Y'; 
        cout << endl << "Sorry, the word is " << origWord << endl; 

       }//end if 
      }//end if 
     }//end while 

     //system("pause"); 
     return 0; 
    }//end of main function 

我相信,我收到此錯誤的原因有事情做與下面的代碼:

 //This provides the letterPlace value with the possition of the 
     //input letter by the user within letterNotGuessedYet 
     letterPlace = letterNotGuessedYet.find(letter,0); 

     //This statement determines if the letter input by the user is 
     //still in the letterNotGuessYet string. 
     if(letterNotGuessedYet.substr(letterPlace,1)== letter) 
     { 
      letterNotGuessedYet.replace(letterPlace, 1, "*"); 
      //cout << endl << letterNotGuessedYet;   //This tests that a letter is being replaced with an * 
     }     
     else 
     { 
      cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl;   
     }// end if 

我很感激任何幫助,你可以把我的方式。

+0

問題是沒有測試它使用'std :: string :: find'函數的返回值。問題是使用'std :: string :: find'函數的返回值而不進行測試。如果沒有找到子字符串,它的[返回值](http://en.cppreference.com/w/cpp/string/basic_string/find)可以是一個特殊值'npos'。如果你在'substr'中使用這個值,那麼它的索引是無效的,這會導致你看到的異常。 – NicholasM

+1

現在是學習調試的好時機。 SO不是調試服務。你需要自己做這個部分。開始你的調試器,指示它停止在exeption拋出站點,檢查你的變量,找出哪一個是哪個數組的外界索引,確定導致這個條件的邏輯錯誤,修復,重複。如果在調試器啓動並正在運行時發生問題,請提出另一個問題。 –

+0

謝謝NicholasM,那個鏈接和你的解釋幫助我理解了發生了什麼。 – Awkt

回答

0

您收到錯誤的原因是因爲您在測試之前使用「letterPlace」值來查看它是否實際找到。如果「string :: find」沒有找到任何東西,它將返回一個值「string :: npos」。在嘗試使用它之前,您需要測試letterPlace可以使用的值。

letterPlace = letterNotGuessedYet.find(letter,0); 

// Check to see if this letter is still in the "letterNotGuessedYet string 
if(letterPlace != string::npos) 
{ 
    // At this point the letter is in the letterNotGuessedYet string so let it go 
    letterNotGuessedYet.replace(letterPlace, 1, "*"); 
} 
else 
{ 
    // The letter was not found which means it has already been guessed. 
    // Show error to the user here 
    cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl; 
}