Bobazonski
Bobazonski

Reputation: 1565

C++ restart main without losing variables

My program's main function displays a switch menu. When option 1 is entered, a function is called that "shuffles" an array of "cards". After the shuffling is complete, that function returns the program to the beginning by calling main(), so that the menu is shown again.

The problem I have with this is that option 4 of the menu writes the shuffled array to a file. But when the cards are shuffled and then the program is restarted, the array data is lost, and therefore the outputted file is all junk. Is there a way to restart main() WITHOUT that data being lost?

I am in a class and am limited in the tools I can use, so only the most basic code will be acceptable. Basically, I'm looking for something like goto but a little safer (goto, by the way, is also forbidden in this class).

Upvotes: 1

Views: 3004

Answers (4)

Cyril Leroux
Cyril Leroux

Reputation: 2629

The main() function should not be called recusively.
You can encapsulate your game loop into a while() function.

Take a look at this example :

#include <iostream>
#include <string>

using namespace std;

int main()
{
    bool exitGame = false;
    // Game Loop
    while(!exitGame) {

        // Display menu
        cout << "Menu :" << endl;
        cout << "- Shuffle :  press 1" << endl;
        cout << "- Option 2 : press 2" << endl;
        cout << "- Option 3 : press 3" << endl;
        cout << "- Exit     : press 4" << endl;
        cout << "Enter your choice : ";

        // Get user input
        string choosenValue;
        cin >> choosenValue;

        cout << endl;

        // Process user input
        if (choosenValue == "1") {
            cout << "You selected 'Shuffle'." << endl;
            // Do cool stuff here !

        } else if (choosenValue == "2") {
            cout << "You selected 'Option 2'." << endl;
            // Do cool stuff here !

        } else if (choosenValue == "3") {
            cout << "You selected 'Option 3'." << endl;
            // Do cool stuff here !

        } else if (choosenValue == "4") {
            cout << "You selected 'Exit'." << endl;
            exitGame = true;

        } else {
            cout << "Wrong value." << endl;
        }
        cout << endl;
    }

    return EXIT_SUCCESS;
}

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126927

Why are you calling main recursively (which, by the way, is forbidden by the standard)? Just use a loop (e.g. a do ... while) to repeat the part of your main that needs to be repeated, keeping the variables that must not be resetted (and their initialization) outside the loop.

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 373102

It's actually not a good idea for a C++ program to call its own main function. In fact, this leads to undefined behavior, which means that you have no guarantees about the behavior of the program. It could crash, or proceed with corrupt data, or format your hard drive, etc. (that last one is unlikely, though).

I think that this reflects a more fundamental problem with how your program works. If you want data to persist across functions, you need to place that data somewhere where it won't get clobbered by other functions. For example, you could put the data in the heap, then pass pointers to the data around. Or, you could define it as a local variable in main, then pass it down into functions and have those functions return when they're done. You could also consider making objects representing the data, then passing those objects across the different functions.

Without seeing your code, I doubt I can give a more specific answer than this. When designing programs, keep the data flow in mind. Think about what data needs to go where and how you're going to get it there.

Hope this helps!

Upvotes: 7

xvorsx
xvorsx

Reputation: 2452

Move main body to another function 'myMain' and call it insteadOf main.

Upvotes: 0

Related Questions