Restart program?

Nov 20, 2010 at 11:01pm
i was wondering if there is a certain code to restart the program resetting all variables and such so like say if i do
1
2
3
4
5
6
7
8
9
10
11
int returnto;

cout << "enter 1 to restart, enter 2 to go to the second menu";

cin >> returnto
if (returnto = 1)
    xxxxx//this is the code to restart from the start
else if (returnto = 2)
    xxxxx//this is the code to return to the second menu
else 
    cout << "this selection is invalid";


but my problem is i dont know what to input to make it restart or continue from another menu like that, any ideas?
Nov 20, 2010 at 11:11pm
Use loops, put the entire main-function body inside a do-while loop and check whether the input equals a value or not. If it equals it, the program will "start over", since it loops back, if not, it ends or goes to a next menu as you said.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   int returnto; // Potentially the only value that should be checkable outside of the "main loop".
   do
   {
      ... // Code here
      cout << "Enter 1 to restart, enter 2 to go to the second menu.";
      cin >> returnto;
   } while(returnto == 1);
   if (returnto == 2)
      ... // Second menu
}
Last edited on Nov 20, 2010 at 11:11pm
Nov 20, 2010 at 11:16pm
i was messing around with this and i seem to have made it loop correctly but it still retains data from the previous entries which kind of is counter-intuitive, it would loop back to the correct place but since the program witheld the previous variable, it goes ahead and continues to the case switch that i set up. Is there anything to make the program forget the variables that were placed previously by the user?
Nov 20, 2010 at 11:34pm
No. You must always initialize your variables properly. Good luck!
Topic archived. No new replies allowed.