weird problem in c++ logical operator ?

i was running this trivial program and whenever i wrote y or e or s it was printing yes! normally, now it doesn't print yes unless i press y only, what's wrong ?

1
2
3
4
5
6
7
8
 #include "stdio.h"
#include "conio.h"
void main ()
{
while ( getche() == 'y' && 'e' && 's' )
printf("\nyes!\n");
}
getche() == 'y' && 'e' && 's' means "if (getche() equals 'y') and ('e' does not equal 0) and ('s' does not equal 0)". Neither 'e' nor 's' are equal to zero, therefore by Boolean algebra[1] the expression is equivalent to getche() == 'y'.
Knowing this, how do you need to modify the expression to do what you intend?


[1] (x and falsetrue) <=> x
Last edited on
i don't know how this worked for me before, but anyway i want the body of loop to be extracted when only the user types y or e or s, no letters else, i want any letter else terminates the program
thanks for help, i have figured out a way !

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "iostream"
using namespace std;
#include "conio.h"
void main ()
{
char c=getche();
while (1) {
if ( c == 'y' || c == 'e' || c =='s' )
{cout << "\nyes!\n";
c=getche(); }
else break;
} 
}
Topic archived. No new replies allowed.