Mar 14, 2014 at 2:57pm UTC
At end of do-while, I would like user to be able to run through switch again.
If user enters 1, it should go back to switch menu
If user enters 2, program should terminate
If user enters anything else, user must be given option to enter 1 or 2 again
How could I fix my code to do what I desire? Thank you.
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
int main(){
int optionChoice, goAgain;
do
{
switch (optionChoice)
{
case 1:
break ;
case 2:
break ;
default :
cout << "Error!" ;
break ;
}
cout << "Go Again?" << endl;
cout << "If so, Enter 1: " ;
cout << "To quit, Enter 2: " ;
cin >> goAgain;
if (goAgain = 2)
{
return 0;
}
} while (goAgain = 1);
}
Last edited on Mar 14, 2014 at 3:56pm UTC
Mar 14, 2014 at 3:16pm UTC
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
int main(){
int optionChoice;
do
{
cout << "Please input a 1 or a 2.\n" ;
cin >> optionChoice;
switch (optionChoice)
{
case 1:
cout << "Go Again?" << endl;
cout << "If so, Enter 1: " ;
cout << "To quit, Enter 2: " ;
cin >> optionChoice;
break ;
case 2:
break ;
default :
cout << "Error!\n" ;
break ;
}
} while (optionChoice != 2);
return 0;
}
Here is a more compressed way to do it if I understand what you are trying to do.
Last edited on Mar 14, 2014 at 4:37pm UTC
Mar 14, 2014 at 3:50pm UTC
I think there was misconception.
optionChoice is for the switch and it actually has around 5 cases.
goAgain is supposed to handle repeating the switch.
Is it the same regardless?
Mar 14, 2014 at 3:52pm UTC
Look at the condition at the end of your do-while loop:
} while (goAgain = 1);
Are you sure you meant you use the assignment operator?
Last edited on Mar 14, 2014 at 3:52pm UTC
Mar 14, 2014 at 3:54pm UTC
@MikeyBoy
The program is supposed to be a temperature converter with 6 different cases.
At end of conversion, I want user to be able to either:
Enter 1 to go back to menu
Enter 2 to quit
And if user enters anything other than 1 or 2, they have to be reprompted to enter either 1 or 2.
Mar 14, 2014 at 3:59pm UTC
I think what MikeyBoy is getting at is that you are trying to compare if two values are equal, not assign a value. =
and ==
are not the same.
Mar 14, 2014 at 4:00pm UTC
@sportstool:
How does that in any way answer the question I asked you?
Look at the condition at the end of your do-while loop:
} while (goAgain = 1);
Are you sure you meant you use the assignment operator?
What is it that you think that line is doing? Are you certain you've used the right operator to achieve what you want it to do?
Last edited on Mar 14, 2014 at 4:01pm UTC
Mar 14, 2014 at 4:05pm UTC
@MikeyBoy
I see ... I was assigning 1 to the int variable runAgain. Thanks for pointing that out.
Last edited on Mar 14, 2014 at 4:32pm UTC