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.

Mohammad_105 commented: By using goto syntax +0

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.

Well, I agree with jonsca, use a while loop. You can also use "continue" to come back to the start of the while loop from anywhere inside the loop.

If it really is not going to work with a while loop, do everything you can think of to make it work... and if all else fails, C++ still supports the infamous "goto" statement.

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.

Calling main results in undefined behaviour, so not really.

commented: Agreed +4

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)
      {
        [...]
      }
    }
    [...]
  }
}

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.

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

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

^ 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;

 while(game.isNotOver()){
  int state = game.getState();
   switch(state){
     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();
      if(playAgain) 
         game = Game(); //re-initialize everything
   }
 }
}
commented: I like it. +4

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.