I am looking for a command that will restart/reload the program. Any help is appreciated.
Also, code to end the program would be nice as well.
I am looking for a command that will restart/reload the program. Any help is appreciated.
Also, code to end the program would be nice as well.
Last edited by GanonsSpirit; 09-14-2007 at 04:47 PM. Reason: Added info.
Just to be sure: do you really want to restart the program or do you simply want to do whatever the program is doing one more time? If the latter, use a looping structure to start execution all over.
To end program, use return in main and exit(n) (I think) in other functions.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
You can probably use system(MyProg.exe) to start the program.
If you wanted the program to re-start itself... Well, the program can't do anything after it ends and exits... You could have the program start a 2nd instance of itself, and then end the 1st instance. (Assuming that you are running Windows or another multitasking operating system that would allow you to run two instances of the same program at the same time... And, assuming there's nothing about the program that prevents two instances of it running.)
anon is right. In most cases, it's better to use a loop. Many programs are written with almost everything inside one big main loop. Inside that loop, you might find more "nested" loops, and several if-statements.
If you are new to programming, I suggest working through all of the tutorials before trying anything new.
Last edited by DougDbug; 09-14-2007 at 05:58 PM.
Right, exit() from <cstdlib>. Pass it an int, which is the return value, just like from main(). Usually you'd use exit(0) (or exit(EXIT_SUCCESS), or maybe exit(1) (or exit(EXIT_FAILURE)) on error.To end program, use return in main and exit(n) (I think) in other functions.
As mentioned, from main, you can just use a return statement.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
In posix you can use
To set the exit handler which is called on process termination. maybe you can pass it your main function to allow your app to resurrect itself all the time. But main has the signature int(*)(void), so it would be a bad hack if it works at all. Better use on step of indirection: write a functionCode:#include <stdlib.h> int atexit(void (*function)(void));
I've never tried this...Code:void handler() { main(); }
Last edited by pheres; 09-16-2007 at 02:00 AM.
Calling main() or taking its address is forbidden by the C++ standard, even if no compiler I know enforces this.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
atexit() is ANSI standard as well as POSIX.
Besides, you'd end up with code like this:
So why not call loop() directly? It would be the same thing.Code:#include <stdlib.h> void loop(void) { atexit(loop); if(condition) exit(0); } int main() { loop(); return 0; }
Also, it would be hard to guarantee that the code would not call exit() again, and both ANSI and POSIX specify that if exit() is called from within a function registered by atexit(), the result is undefined. So with that code, you'd only be able to restart the program once.
Not to mention that it's a recursive solution so that you'd probably* eventually run out of stack space eventually.
* Some compilers can optimise recursive code into iterative code.
In short, that is a very bad idea.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ok, I probably eventually got it eventually thx