do while problem, please help

Jun 27, 2015 at 2:24pm
Hi! I want to know what's my mistake in my code but in myself, i know, I haven't done anything wrong. So here's my question about the || operator, please see my code. I know i did correct but when I enter "x" or "X", it doesn't exit properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>
#include <string>
using namespace std;




int main() {

char input;



do {

cout << "Welcome to learning lad" << endl;
cout << "This is where you can learn much more" << endl;
cout << "If you wish to exit, press x, if not, press any other key" << endl;
cin >> input;



}  while( input != 'x' || input != 'X');







return 0;
}







Last edited on Jun 27, 2015 at 2:24pm
Jun 27, 2015 at 2:29pm
1
2
3
// while( input != 'x' || input != 'X');
while( input != 'x' && input != 'X'); // input is not 'x' and input is not 'X'
// ie. input is not one of { 'x', 'X' } 
Jun 27, 2015 at 2:32pm
Your condition is flawed. Now it means
"repeat if input is not equal to 'x', or if input is not equal to 'X'"

Look what happens when you enter 'x':
1
2
3
4
input != 'x'false
input != 'X'true

false || truetrue → repeat


You want && here.
Jun 27, 2015 at 3:46pm
What i thought here is, if x is intered the bigger X will be flushed, if the bigger X was entered, the condition for the lowercase x will be flushed.
Last edited on Jun 27, 2015 at 3:52pm
Topic archived. No new replies allowed.