Trouble looping back to main menu

I'm trying to get my program to ask the user if they would like to return to the "main menu" after it has completed a calculation, but I can't figure out how to make it return to the top of the code.

Here's my current code: http://pastebin.com/QS040bij
You can use a do-while loop. e.g:

1
2
3
4
5
6
7
do
{
    //code
    //in
    //here
}
while(returnToMainMenu == 'y');


....where returnToMainMenu is a variable.
Last edited on
With your advice I'm doing this :

1
2
3
4
5
6
7
int main()
{
     do
     {
          //code
     }
     while (returnToMainMenu == 'y');


and I'm receiving this error:

1>c:\users\USERNAME\documents\visual studio 2010\projects\c programming practice\calculator\calculator.cpp(173): error C2065: 'returnToMainMenu' : undeclared identifier

which I believe is due to the fact that the returnToMainMenu variable is being declared inside the if statements individually, not outside.
Here's a link to the code that gives me that error (includes the do-while loop suggestion):

http://pastebin.com/KPZVn8j7

Thank you for your help
Remove line no 155 to 171 it's useless and declare
char returnToMainMenu; to the outside the if condition.
closed account (j2NvC542)
You should transfer the code for addition/subtraction etc. into seperate functions. It will make it simpler to read.
Get rid of the return 0; at the end of addition/subtraction etc. as it makes the program end immediately.
Then you should put the do while loop for invalid input in an else statement.
You need to declare a char variable "returnToMainMenu" and make the user input 'y' or 'n'.
Thank you everyone for the replies. I got it working.
Topic archived. No new replies allowed.