This is another thing I have personal hatred of, the != && != business... |
-------
there are so many other ways to do that.
as people said, a switch is nice, the fall-through handles case perfectly.
you can also just multiply or add expressions...
if( !(x == 'y' + x == 'Y' + x == 'n' + x=='N')) //simplified, which can shrink with toupper..
for letters specifically but you can generalize the idea:
bool isvalid[256]{}; //any container will do here. maps, sets, and others can be
//more space efficient, as can vector bool (using just a few bytes for it all)
isvalid['y'] = isvalid['Y'] = isvalid['n'] = isvalid['N'] = true;
... if (!isvalid[input])
mix and match, a bool function with a switch or toupper also results in a simple
if(isvalid(input))
and that is more usual for string/number/larger input validations. You don't want to do this in place every time, you want a little library of validation routines for all the common ones that show up over and over..
and even better: ALL THIS STUFF GOES AWAY once you get to UIs. Its the console I/O that creates most of the woes. Give em a yes and no button and they can't F it up.