1

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).

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Bobazonski
  • 1,515
  • 5
  • 26
  • 43
  • Calling `main` in C++ is illegal. Which compiler allows that? – chris Feb 08 '13 at 02:58
  • 2
    "I'm looking for something like goto but a little safer" this sounds very much like the description of looping statements (`while`, `do`, `for`)... – Matteo Italia Feb 08 '13 at 03:00
  • @chris, calling main is not illegal. you can do it in gcc. it most likely has a different effect than what you probably want.... but it's ok. try this code: int main(); int a() { return main(); } int main() { return a(); } – thang Feb 08 '13 at 05:12
  • remark that while the standard doesn't allow it, it seems every compiler that i know of does: http://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c – thang Feb 08 '13 at 05:18
  • @thang, I guess it's because I always had pedantic warnings as errors, which is something I recommend doing anyway. It's illegal per the standard, and that's the defining factor. – chris Feb 08 '13 at 05:23
  • @chris, Interesting that -Wall doesn't even cause it to generate a warning or error. you have to do -pedantic to get a warning. it seems to me that if the standard explicitly forbids it, compilers should at least put a warning up by default.. – thang Feb 08 '13 at 05:24

4 Answers4

7

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

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;
}
Cyril Leroux
  • 2,599
  • 1
  • 26
  • 25
0

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

xvorsx
  • 2,322
  • 2
  • 18
  • 19