Hello there! So I'm having a hard time replacing a character in a masked word based on the results of a string find function. The user guesses a letter, I determine whether or not its in the word with a string find function and then determine what index the correctly guessed letter is in. I'm pretty sure I'm not allowed to use mask[string.find(guess] in my code but I don't know how else I'm supposed to do it! /Does mask need to be a char array for it to work? Mask is declared as a string. This is only a portion of code from my hangman game. Thankyou for your time!
cout << wordToGuess << endl;
//creating a masked version of the word to guess
for (int i = 0; i < word.length(); i++)
{
mask += " _";
}
cout << "\nWord to guess: "<< mask;
// Input Game Check(was the letter that they guessed in the word?)
if (word.find(guess) != string::npos)
{
cout << "\nGREAT JOB!";
cout << "\n" << word.find(guess) << endl;
guess = toupper(guess);
cout << "\n" << guess << " is in the word to guess!\n";
//Verifying that the found index that equals guess is correct
cout << word.find(guess);
//Setting the index in the string mask to equal the guess
mask[word.find(guess)] = guess;
//Displaying what should be the new masked word with the guessed letter revealed
cout << mask << endl;
++correctGuess;
if (word.length() == correctGuess)
{
cout << "\nYOU WIN!\n" << endl;
break;
}
else
{
cout << "" << endl;
}
}
Why do you think you are not supposed to use mask[word.find(guess)];?
If you really dont want to use it, do a linear search on the word to get the index of guess.
I get an outside error, its like it just really doesn't like when I use mask[word.find(guess)]; for some strange reason. You basically just got down to the root of my question and re-asked it if that helps you understand :)