Telling my program to return to a previous line of code?

Apr 7, 2014 at 3:09pm
Hey guys. I'm a total noob and wanted to know something, if you could spare some time for me :). So, how do I make it so my program repeats a block of code again in the program? For example, let's say that I'm making a puzzle game, but every time the user makes a mistake he has to start all over again from the last checkpoint or so. I want to tell my program to send to user "back in time" so that he has to play the game all over again.. How do I do that without using a for/while loop? Thanks! :)

Apr 7, 2014 at 3:12pm
How do I do that without using a for/while loop?

Why do you not want to use a loop?
Loops are the primary mechanism for doing something more than once.

Apr 7, 2014 at 3:43pm
Why do you not want to use a loop?
Loops are the primary mechanism for doing something more than once.


Well because, say if it's a really, really long game and I wanted to make a "restart" button, how do I tell my program to start at the beginning again? Many thanks! :)
Apr 7, 2014 at 4:10pm
What kind of program are you writing?
1) A console application?
2) A GUI? (Windows, QT, SFML, etc).

1) If it's a console application, you're not going to have buttons. You can prompt the user if they want to restart or determine programmatically that you want to restart, and set a restart flag. Your code then needs to check that flag periodically. If it's set, you want to exit out of what ever you're doing back to your outermost loop which then returns to the top and starts a new game.

2) If it's a GUI, then you have buttons and event handling facilities, but the process is pretty much the same. When the button is pressed, a button press event occurs. In the button press event handler, you set the restart flag. You still have to check the restart flag and return to the outermost loop.
Apr 7, 2014 at 4:31pm
What kind of program are you writing?
1) A console application?
2) A GUI? (Windows, QT, SFML, etc).

1) If it's a console application, you're not going to have buttons. You can prompt the user if they want to restart or determine programmatically that you want to restart, and set a restart flag. Your code then needs to check that flag periodically. If it's set, you want to exit out of what ever you're doing back to your outermost loop which then returns to the top and starts a new game.

2) If it's a GUI, then you have buttons and event handling facilities, but the process is pretty much the same. When the button is pressed, a button press event occurs. In the button press event handler, you set the restart flag. You still have to check the restart flag and return to the outermost loop.


Ty for the reply.
It's a console application, and I literally started programming 2 days ago haha, I don't know much. What's a restart flag? Ty!
Apr 7, 2014 at 6:18pm
What's a restart flag?

It's simply a variable (typically a bool) that you set when you want to restart.

 
  bool restart_flag = false;

Apr 7, 2014 at 6:26pm
2 days ago? Then we can use functions!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void bool mygame()
{
  // your game here
  if (exit_is_pressed)
    return false;

  // more game here   

  return true;  // Return true to do it again.
}

int main()
{
  while ( mygame() )  {  }
}


This will cause the game to loop. If you want to go to the next loop, you c
Last edited on Apr 7, 2014 at 6:43pm
Apr 7, 2014 at 6:32pm
@stewbond - You probably want to make mygame() a bool function.
Apr 7, 2014 at 6:44pm
Yep, you're right. I originally wrote it as "void" with exceptions, but realized that was silly.
Apr 8, 2014 at 11:31am
Ty everyone! :)
Topic archived. No new replies allowed.