Hello everyone;
I'm trying to do something which I'm not sure if it is available in c++. Actually, I'm developing a game with functions.And to reset the game when the user presses a button or a certain event occurs, the program must start running from all over, say from the very first line of the code. Is it possible to go back some number of lines while keeping the global constants,func.declarations intact, or simply start running from the scratch? Although I'm developing a game, I posted this thread here in c++, because I thought it was more relevant to c++ then games development.
Thank you for your valuable answers.
Best regards.

Put your code in an infinite while loop (using while(true) ) and break out of it when you want to exit. All of your function declarations would stand anyway and global variables would persist during the program execution, so I'm not sure what you mean by that.

You can also call main() recursively from anywhere in your program. This could be a good or a terrible idea depending on what you're doing.

But there already is an infinite loop in the main and it accommodates a switch statement which calls functions according to the user presses key. So,after I break out of the statement, the main and the program terminates. I think "goto" may work in this situation as I have read so far, not tried yet, because I need to go back to the starting line of the main.Thank you for your concerns mates.Btw "auburnmathtutor" if you are from NSW, hi from Newtown :)

You can have more than one loop per program, you know.
You don't need goto.

int main()
  for (;;)
  { //main loop
    for (;;)
    { //user input loop
      switch (userinput)

Edited 6 Years Ago by Aranarth: n/a

Calling main results in undefined behaviour, so not really.

Huh. I wasn't aware of that... it looks like you're right though, you're not supposed to do it in C++ though it's legal in C. And it works for me when I try it on g++. Weird... whatever. I don't want to get too far off-topic, but was there any compelling reason for the change, other than the nagging feeling that having a recursive main is a funky bad idea?

It's sort of a non-issue though, because you can always have main() call a dummy main2() function immediately, and you can certainly call main2() recursively. You can even pass main2() the command-line arguments.

But of course even that is something of a non-issue, because in principle I agree that looping probably is the better solution.

Edited 6 Years Ago by AuburnMathTutor: n/a

Try to do it when compiling with -pedantic-errors.
As for the corresponding part of the C++ standard:

( Recursive calls are permitted, except to the function named main (3.6.1).

Edited 6 Years Ago by Aranarth: n/a

^ No I believe you, but do you know why that rule was made when it wasn't a rule at all in C? At least I don't think it was a rule in C. What changed?

@OP: You should do something like this :

int main(){
 Game game;

  int state = game.getState();
     case PAUSE_GAME : pauseGame(game); break;
     case PLAY_GAME : playGame(game); break;
     case EXIT_GAME : cleanUp(game); break;
   if(!game.isNotOver){ //if game is over
      bool playAgain =  checkIfUserWantsToPlayAgain();
         game = Game(); //re-initialize everything
I like it.

@firstPerson thank you mate.It seems like the thing I was after.
Thank you all for the valuable replies.Much appreciated.

This article has been dead for over six months. Start a new discussion instead.