hello, I have the below code which asks the user for input into a string, then checks whether that string is present in a vector of strings (member of class) to make sure the guess is valid. It all works great if you put valid guesses in, but the moment you put an invalid guess, which prompts the INVALID PASSWORD msg to display, even if you enter a correct guess after, you will always get an invalid password.
I don't understand why that is since I do cin.clear, cin.ignore and getline calls s.erase()?
Do you understand by that ignore() ignores a single character by default? Perhaps you should ignore the entire line instead of a single character.
Also when dealing with a string or a single character from cin the only "normal" cin failure condition is someone entering eof() instead of a string or character.
Have you considered running the program with your debugger, single stepping through the problem sections watching the variables as you step?
Lastly your guess_valid() function seems overly complicated.
It would help us help you if you made a minimal, compileable example that reproduced your issue. As it is now, we have to take the effort to fill in the gaps ourselves.
Anyway, you shouldn't just put cin.ignore() calls everywhere. If you're only ever using getline and not std::cin >> operator, then you don't need cin.ignore() calls at all under normal circumstances.
If you used a --> debugger <--, you would see that the value of s after your line 28 would be "ello" if you typed "hello".
Remove the cin.ignore() calls.
And by the way passing a std::string by value is often considered a bad practice, instead consider either passing a std::string view or passing the string by const reference.