Click here to Skip to main content
13,455,913 members (49,129 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want reset my program. for example if I press key R my program start from begin
how can I do that ?
Posted 23-Dec-12 2:29am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


For a console application:
At the top of your code file, add this:
#include <conio.h>

In your main method:
bool stop = false;
while (!stop)
      system("cls"); // clear console
      // run your program
      int pressedChar = getch();
      stop = pressedChar != 114; // 114 = 'R'
return 0;

For a Windows application:
Have a look here:[^]
How To Create a Self-Restartable Application[^]

Hope this helps.
Abhishek Pant 23-Dec-12 8:45am
ProgramFOX 23-Dec-12 8:46am
Thank you!
mohammadali1375 23-Dec-12 9:20am
thanks. but my program is'n a console program. its a 2d game with sdl and have alot of class and vars. i want after the game end , my program re start for play again.
ProgramFOX 23-Dec-12 9:21am
Thanks for your reply!
I'll try to find how to restart a Windows application.
mohammadali1375 23-Dec-12 9:55am
Thanks for your help
ProgramFOX 23-Dec-12 9:56am
You're welcome!
Richard MacCutchan 23-Dec-12 11:48am
Your expression is wrong (should be ==), and why not use the actual character constant rather than a decimal number, thus:
stop = pressedChar == 'r';
ProgramFOX 24-Dec-12 5:36am
I agree that it's better if I use 'r' instead of 114, but I don't agree with the <small>==</small> operator. While <small>stop</small> is false, then your program must run. <small>stop = pressedChar != 'r'</small>, so <small>stop</small> is true if the pressed char is 'r', and false if the pressed char isn't 'r'.
Richard MacCutchan 24-Dec-12 5:54am
No, in the above expression stop becomes true if the pressed character is not equal to 'r'. Try it for yourself.
ProgramFOX 24-Dec-12 6:01am
No, <small>stop = pressedChar != 'r'</small>, so <small>stop</small> becomes <small>true</small> if <small>pressedChar != 'r'</small>. And the application must stop if the pressed char isn't 'r'.
Richard MacCutchan 24-Dec-12 6:06am
1. That's what I said.
2. Read the question.
ProgramFOX 24-Dec-12 6:10am
I did read the question. if I press key R my program start from begin, so if the pressed char isn't 'r', then the application doesn't start from begin, but then the application terminates, and if the pressed char is 'r', then the application starts at the begin of the while loop.
Richard MacCutchan 24-Dec-12 6:26am
So with your code if the user presses any key the program terminates. I don't think that is what the OP asked for. What he wants is for the program to run and if 'r' is pressed then it should restart from the beginning.
ProgramFOX 24-Dec-12 6:30am
Yes. If you press the 'r' key, the program starts from the beginning. But if another key is pressed, then the program ends, because my program asks for input at the end of the while loop. If 'r' is pressed, then you start again at the begin of my while loop.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You need to add a method to your main class that purges all views and documents and resets all control variables to their initial state. However, only you know what those things are.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180322.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100