Hi there,
I am writing a program and I need input from it. But I only want to receive input if it is either 'y' or 'n'. I tried writing an if statement that checks the output like this
With that code it comes up with the error message and works if you type in 'y' but not 'n'.
So I need some code to pick up input and ask the user to try again if it isn't correct.
I have heard about try() throw() and catch() and I tried reading the tutorial on them but it just confused me so I didn't know if thats what I needed to look for.
by the way, if you choose the first way, you must either initialize check_steam to 0, or use do cin >> c; while(c != 'y' && c != 'n'); to prevent random errors.
it would make sense to initialize check_steam to a char because that's what he's checking... just say, but yes in the statement it doesn't know what your supposed to be checking 'n' with. I think if you were to use the || method it might cause an infinite loop for some reason, cuz whenever i've tried using || in a while loop it throws it into a infinite loop, i'm not sure why though.
Of course it loops for ever. It loops until c != 'y' and c!='n' both return false, but when c == 'y', c !='n' and vice versa. The loop with || would only stop if c could be 'y' and 'n' at the same time.
Those or logic operators are killing you. You need to say while ((c != ' y') && (c != 'n')).
I'd also make sure anticipate the user entering capital letters.
while( c != 'y' || c! = 'Y' && c != 'n' || c != 'N' )
int !=, ! and = cannot be separated by a gap.
&& has higher precedence that || so what you wrote is equivalent to while( c != 'y' || (c != 'Y' && c != 'n') || c != 'N' ) which would work when c is 'Y' of ' n', but not 'y' or 'N'. If you add parentheses where they should be (while( (c != 'y' || c != 'Y') && (c != 'n' || c != 'N') ) it should work fine.