I do not see it

Whats wrong with this code it will allays run no matter what I put
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
char Menu()
{
	char ch; // store menu choice

	do
	{
		cout <<"\nWhat do you want to see?\n" << endl;
		cout <<"r - rod\n";
		cout <<"c - chain\n";
		cout <<"t - tree\n";
		cout <<"d - dart\n";
		cout <<"S - Septic\n";
		cout <<"C - cubic\n";
		cout <<"A - all\n";
		cout <<"x - Exit.\n";
		cout <<"Enter your choice: ";
		cin >> ch;
		cin.ignore(100,'\n');
		
		/*if (ch!='r'||ch!='c'||ch!='t'||ch!='d'||ch!='r'||ch!='C'||ch!='S'||ch!='x')
		{
			cout <<"Wrong input, try again please." << endl;
		}*/
	}while (ch!='r' || ch!='c' || ch!='t' || ch!='d' || ch!='r' || ch!='C' || ch!='S' || ch!='x'); 

	if (ch == 'x')
		exit(1);

	return ch;
}
Last edited on
ch!='r' || ch!='c' || ch!='t' || ch!='d' || ch!='r' || ch!='C' || ch!='S' || ch!='x'
for this to be false it means all of ch!='r', ch!='c', etc. must be false. That can never happen because ch!='r' is only false when ch=='r' but then ch!='c' is true so it can never happen that all of them is false at the same time.
Last edited on
does this apply the same for the if statment?

If i do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

bool choice = false;

if (ch=='r'||ch=='c'||ch=='t'||ch=='d'||ch=='r'||ch=='C'||ch=='S'||ch=='x')
		{
		           choice = true;
		}

// I change the while to 

do
{
//.....
}while (choice);
anyone?
Yes. You should use && (and)
I think you need/mean

}while (ch!='r' && ch!='c' && ch!='t' && ch!='d' && ch!='r' && ch!='C' && ch!='S' && ch!='x');

That is, you're testing that it isn't r and it isn't c, etc. This will evaluate to false if any one test fails. i.e. it is one of r, c, ...

But you could also use

}while (NULL == strchr("rctdrCSx", ch));

strchr
http://www.cplusplus.com/reference/clibrary/cstring/strchr/

Andy

P.S. You're repeated 'r'
Last edited on
The problem in your program is clear. Instead of running the while loop as long as the input is incorrect (i.e. input other than r, c, t, d, S, C, A, x), you are running the loop for correct inputs. This happens in both, your while loop, as well as your above if loop. Try this:

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
Char Menu()
{
	char ch; // store menu choice

	do
	{
		cout <<"\nWhat do you want to see?\n" << endl;
		cout <<"r - rod\n";
		cout <<"c - chain\n";
		cout <<"t - tree\n";
		cout <<"d - dart\n";
		cout <<"S - Septic\n";
		cout <<"C - cubic\n";
		cout <<"A - all\n";
		cout <<"x - Exit.\n";
		cout <<"Enter your choice: ";
		cin >> ch;
		cin.ignore(100,'\n');
		
		
	}
        while (ch!='r' && ch!='c' && ch!='t' && ch!='d' && ch!='r' && ch!='C' && ch!='S' && ch!='x'); 

	if (ch == 'x')
		exit(1);
        else
	return ch;
}


In OR logic, if even one of the conditions is true, statement is true. So suppose input is r, ch!='r' is false, but rest were true. So statement result was true, and the while loop re-iterated. This is what was causing the infinite loop. But AND logic is, if even one of the statements is False, the result is False.
Hope this helps!
thanks everyone for the time and help
Last edited on
Topic archived. No new replies allowed.