如何保持跟踪字母已经猜到了在猜字游戏为c++

How to keep track of letters already guessed in Hangman game for C++?

本文关键字:c++ 游戏 何保持 跟踪      更新时间:2023-10-16

这次给我的作业是抄袭课本上的一个猜字游戏,并按照书上的具体说明修改它。我花了很多时间来研究这个问题,并找到了为什么我总是得到相同的错误信息的原因。在我所看到的任何地方,每个试图修改这段代码的人都尝试过使用数组,并且都有和我一样的运气。我目前正在做字符串中的一章,并计划做我的大部分语句,这是由字符串中的指令请求的。

在这段代码中我需要修改的是:

  1. 记录用户输入的所有字母
  2. 如果用户输入已经输入的字母,则向用户发送错误消息
我遇到的主要问题是,当我编译这段代码并放入我已经放入的相同字母时,它以消息结束:

抛出'std::out of range'实例后调用终止(): 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

我很感激你给我带来的任何帮助。

您得到错误的原因是因为您在测试之前使用了"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;
}