给我的这项任务是从我们的教科书中复制一个Hangman游戏并将其修改为书中的具体说明。我花了很多时间试图研究这个问题,并找到一个原因,我一直得到与我相同的错误信息。我看过的每一个地方,每个尝试修改此代码的人都试图使用数组并且拥有与我相同的运气。我目前正在编写Strings中的一章,并计划完成我在String中的指令所要求的大部分语句。
我需要在此代码中修改的内容是:
我遇到的主要问题是,当我编译此代码并输入我已经放入的相同字母时,它会以消息终止:
在抛出'std :: out of range'的实例后终止调用 what():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 :(得分:0)
您收到错误的原因是因为您正在使用&#34; letterPlace&#34;测试之前的值,看它是否真的被发现。如果&#34; string :: find&#34;没有找到任何会返回值的东西&#34; string :: npos&#34;。在尝试使用它之前,您需要测试letterPlace vairable值。
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;
}