I need to compare a string's letters to another char (a guess in my hangman program). The string temp; comparison bombs out and stops working in the if statement after a user enters two wrong guesses. I believe it is a problem using the .at() string function. Is there another way to pull out a character at a location in my string and make a comparison using that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for(int b = 0; b < Word.newWord.length() ;b++) //runs for all spaces in newWord. newWord is the word to be guessed.
{
for(int a = 0; a < Word.newWord.length(); a++) //same as above
{
string temp;
temp = Word.newWord[a];// Word.newWord.at(a) doesn't work either - causes same problem
if(Word.alphabet[b] == temp) //if user guess and word match
{
output[a]= Word.newWord[a] ; //Word.newWord.at(a) doesn't work either - causes same problem
//str2 = str2+ Word.newWord.at(a);
}
} //for end
}//for end
at() is checked access. If that causes a problem, so will unchecked access -- it will just be harder to track the problem down.
We don't know types for your code snippet, so it's difficult to understand what you're trying to accomplish and what should work.
I'm assuming word.newWord is a std::string representing the word to be guessed, and word.alphabet is a string representing ?
output, which I would assume is a std::string turns out to be an array of std::strings which is not what you want. One std::string will serve. The type of Word is not apparent. You need to supply at least the type definitions for meaningful advice concerning your existing code. (of the form class XXX { ... };