Hello, I am new to computer science and I have a problem with my code. I posted part of my code below and hope someone can tell me what I'm doing wrong. Basically, whenever the program reads a character that isn't 'Y','y','N',or 'n', it will go in the while loop and cout "Type etc..". However, when I run this program, it will go into the while loop even if I input Y or y or N or n. (Not able to get out of the loop) Can someone tell me what I'm doing wrong here. Thanks
1 2 3 4
cin >> dec;
while (dec != 'Y' || dec != 'y' || dec != 'N' || dec != 'n')
{ cout << Type 'y/Y' or 'n/N'" << endl;
cin >> dec;}
x || y evaluates to true if at least one of {x, y} is true. If dec == 'Y' then obviously dec != 'y', and if dec == 'y' then dec != 'Y'. All expressions of the form x != y1 || x != y2 || ... || x != yn are tautologies. That is, they're true regardless of the values of x and y1, ..., yn.
With one exception: y1 == y2 == ... == yn. Then the expression is equivalent to x != y1.