Is there a function that will restart the program at the main function?
Printable View
Is there a function that will restart the program at the main function?
put a do{}while() loop or just a while() loop at the start of main and at the end of whatever you want.
For example:
Code:...
int main()
{
bool quit = false;
while(quit == false)
{
...whatever you want to repeat
(when you want to quit just set the quit flag to true)
.....
}
return 0;
}
Good luck!
is there something to use besides a do while loop to restart the program. Maybe a function call?
why did you make another thread? Well my answer's there =P
The loop is the appropriate way of doing it.
oh really? so a loop will call all constructors of static variables and re-initialise all the C runtime stuff?
kellymart, the problem with restarting a program is that as soon as you call the function to exit the program... well the program exits, so your restart won't get called. This is why we tend to avoid restarting programs. However, if you REALLY need to restart, one way is to spawn another program that will spawn your program back again.
The really bad way to do it is to call the main function. Although some real old compilers allow this, it is not advised.
Go with Chaos Engine's theoy instead.
Oh, on a side note, using a loop would only repeat the section of code until you break the loop. Unless you put a loop round the whole code ( not advised ) then you could call the function that main calls first to set reset or set up the variables in a recursive mannor. To be honest, restarting a program is hardly done in practice
My guess is that spawning another process is overly complicated for what kellymart87 wants.
If you can do it with a loop, then you should definitely prefer a loop over any other option. You can always move code into a separate function or class to make the loop look nicer and clearer, but in the end you will probably find the loop to be the best option.
Yeppers, that's what i told you from square one ;) haha
well, maybe if the OP said why he wanted to restart we could make a better judgement. If you read my post, you'll notice I specifically said restarting is in general to be avoided. However, there are some case where it's unavoidable (or at least, so much hassle that it's easier to restart).
Yeah i know what you're saying.
I use 'restarting' (to use it loosely) all the time with programs which have menus. For instance, the whole program (for the most part cept for certain variables) is in a while loop. Each time the loop is called it displays the menu, one of the menu options is quit which breaks the loop. That's the only time i've needed to use a 'restart.'
So yes, could we perhaps know Why you need to restart the application?
Thanks!
In case user finds the virus and wants to stop it.
???
Now i'm lost.
The poster PM'ed me and the problem is fixed =)