does anyone know why this code wont work? the pass word is 'mama' and it is not case sensitive, its purpose is to say invalid as soon as one wrong character is inputted.
Your original if statements were faulty in that they were always true. You said to report the character as invalid is if it was not 'm' or not 'M'. Since it can't be both, it has to be not one or the other, so it is always invalid. In your new code, you're going to be resetting the validity every time you input a character, which is probably not what you want.
Well, now your 'valid' logic is wrong and you're using a non-standard unbuffered console input function and mixing it with fflushing stdin. I'd say you've gone downhill considerably since your first post.
constchar password[] = "MAMA" ;
enum { N = sizeof(password) - 1 } ; // number of characters in password
for( int i = 0 ; i < N ; ++i )
{
if( std::toupper( getche() ) != password[i] )
{
std::cerr << "\nInvalid password, please try again.\n" ;
i = -1 ; // repeat the for loop all over again
}
}