This part of the function is where the action happens.
The user has already inputted the word (word) and is now guessing the letter (guess)
The guess should be in the word... and then output the number of times that letter occurs so the user cannot enter it.
However before I get to that I need it to work and this does not work properly.
If the word is "hello" and I enter 'o' it comes up with "no" and is wrong. But o is in the word hello....
How do I output the position then? I want this to find the letter and put it on the word like hang man and then the user can guess another letter but not search for the same letter twice or more if it is the word more than once.
while(w=input.find(guess) < input.size()){
// If guess was found, then this code will be executed.
}
// Otherwise, w will become npos which Should be greater than input.size().
// The loop will exit.
1 2 3
while(w=input.find(guess) != string::npos){
// This will also work.
}
(If anyone notices an error, or I am blatantly wrong, please say so. I haven't tested this code, but it looks like it will work to me.)
um.. no lol. w!=string size. w=input.find(guess), which is the position of a string or char in a string. If find() could not find the string or char in a string, it returns npos, which is some maximum value. Not really sure what that means, but here: http://www.cplusplus.com/reference/string/string/npos/
So, this means w is not counting the correct answers. w is holding the position of what is found, whether that be the position of an element of the string, or npos.
w is a variable I created which gets bigger by 1 each time a letter is found.
When the value of w is the same as input (the word) then it exits the loop.
My problem is, that I need to print out where the char (guess) is in the word so it looks like a proper hangman game.
The code works...
I just need the position of the found chars so I can output them. Remember the game is hang man so the user needs to see the word as it builds up.
the find() method is what gives you the position. Which is why i set w to it. Make a different variable for position and set it to input.find() then check to make sure it isn't longer than the string's length.
if( (p=input.find(guess)) < input.size()){}
is the same as
if( (p=input.find(guess)) < sting::npos){}
in terms of what you want.
Now, the reason I used while in my example is because you want to check for multiple letters. As long as p < input.size() OR npos, do the code in the block.