Skip to the main function from another.

I made a console application and defined a function given below to exit the program. It confirms if the user really wants to exit by making him enter either yes or no. If he enters yes the program will quit nicely but if he enters no I want the program to return control to the start of the main function.
How can i do that ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    void exit()
    {
        string choice;

        startExit:
        cout << "Do you want to exit? yes / no" << endl;
        cin >> choice;

        if (choice == "no")
        {
            cout << "Returning to start...";
            // What to use here??
        }
        else if (choice == "yes")
        {
            cout << "Exiting..." << endl;
            cout << "Press Enter to continue..." << endl;
        }
        else
        {
            cout << "Enter either yes or no!" << endl;
            goto startExit;
        }
    }
You can't do that in a single function that doesn't care about program structure.

Clearly something will need to call that function, so there is some structure in place. So you need to do the control there. Do not attempt to jump between functions in that way.

Perhaps if you could describe how your code works, someone could suggest a clean way to accomplish this.
Topic archived. No new replies allowed.