menu choices (interpretation?)

Hi
I have been trying to create a menu with the following code. The menu here is made of 2 choices. However if I give for choice=1, I get the error message (L15) and I can't leave the inner "while"-loop.
I tried removing from the while loop's statement the << || choice!="2" >>-part on L12 and then it was able to recognize my choice "1" and get me out. So I think that the problem is in that while loop.

Any idea of how I could fix it?

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
#include<iostream>
#include<string>

using namespace std;

int main(){

char ans;
do{

string choice="0"; //assign a string to enter the next loop
while(choice != "1" || choice !="2" ){ //checks if string is not 1 or not 2 & enters loop
cout<< "Would you like to calculate: "<<endl <<"1. integral" <<endl <<"2. derivative";
cin>>choice;  //assign new string to choice
if(choice !="1" || choice!="2"){cout<<"Please give a legit answer";} //if choice is not 1 or not 2 print error
} //end of loop

if(choice=="1") { cout<<"You chose 1" ;}//TESTING write code to solve the integral 
if(choice=="2") { cout<<"You chose 2";} //TESTING write code to solve the derivative 

cout<<"Would you like to retry?(y/n)";
cin>>ans;
}while(ans=='y' || ans=='Y');

if(ans=='n' || ans=='N'){cout<<"Thanks for trying me";}

system("pause");
return 0;
}
Last edited on
When reading in strings, I usually use the string function getline, because it "eats" the newline character. From the looks of it, probably the reason you have a infinite while loop is because of the newline character. On line 15, I would change the || to &&, because if choice is "1", then the boolean expression will evaluate to true. If choice is "2", then it will also evaluate to true.
Last edited on
Thanks for the getline() feedback.
The program works fine when instead of the OR (||) operator I use the AND (&&) operator both in the while and the if...
Now I am trying to understand why is that the case...it's really weird since not 1 or not 2 sounds the same as not 1 and not 2...I guess it has to do with logic.
It has to do with the truth tables.

When evaluating boolean expressions, C++ uses short-circuit evaluation.
For example:

if(a == 1 && b == 2)
//Do Something

If a is not equal to 1, then the expression b == 2, will not be evaluated, since according to the truth tables when using &&, the whole expression must be true.

If a is equal to 1, then the expression b == 2 will be evaluated and if b really is 2, the the whole expression is true, else it is false.

if(a==1 || b==2)
//Do Something

If a is not equal to 1, then the expression b == 2, will be evaluated, since according to the truth tables when using ||, only one expression must be true.

If a is equal to 1, then the expression b == 2 will not be evaluated, thus the expression will evaluate to true.

Hope this helps.
Last edited on
Yes it does... thank you :)

May I ask one more thing? Will the perfomance become better if instead of assigning a string (L11) and a while-loop right after, I use a "do-while" at L12-16 ?
I think I'm doing an extra unneeded iteration as it is.
The performance is the same either way. The boolean expression is checked first in a while loop and last in a do-while loop. So one rule to use is if there is a chance that something may never be true, use a while loop and use a do-while loop when you know that something should happen at least once. In your case,

1
2
3
4
5
6
7
8
9
10
11
12
13
14

string choice;

do
{
       cout << "Would you like to calculate\n:"
              <<"1. integral\n"
              <<"2. derivative\n";
       getline(cin, choice);

       if(choice !="1" && choice!="2")
             cout <<"Please give a legit answer";

}while(choice != "1" && choice != "2");
Topic archived. No new replies allowed.