Click here to Skip to main content
11,496,146 members (515 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C++
Hi.
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 3:29am
Hi,

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:
http://www.cplusplus.com/forum/windows/42674/[^]
How To Create a Self-Restartable Application[^]

Hope this helps.
  Permalink  
v3
Comments
Abhishek Pant at 23-Dec-12 8:45am
   
+5
ProgramFOX at 23-Dec-12 8:46am
   
Thank you!
mohammadali1375 at 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 at 23-Dec-12 9:21am
   
Thanks for your reply!
I'll try to find how to restart a Windows application.
mohammadali1375 at 23-Dec-12 9:55am
   
Thanks for your help
ProgramFOX at 23-Dec-12 9:56am
   
You're welcome!
Richard MacCutchan at 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 at 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 == operator. While stop is false, then your program must run. stop = pressedChar != 'r', so stop is true if the pressed char is 'r', and false if the pressed char isn't 'r'.
Richard MacCutchan at 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 at 24-Dec-12 6:01am
   
No, stop = pressedChar != 'r', so stop becomes true if pressedChar != 'r'. And the application must stop if the pressed char isn't 'r'.
Richard MacCutchan at 24-Dec-12 6:06am
   
1. That's what I said.
2. Read the question.
ProgramFOX at 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 at 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 at 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.
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2015
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