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.
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);
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:
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);
elsereturn 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!