View Full Version: How to restart the program in C

C++ Learning Community > Other Programing Languages > How to restart the program in C


Title: How to restart the program in C
Description: I need to restart my C program


Dixon - August 29, 2003 06:19 PM (GMT)
I made an American Standard to metric converter in C but right now after you get your answer I have the
system("Pause");
return 0;
So of course it says "Press any key to continue and then closes the program. How can I make it so that it goes back to the beginning of the program (like restarting it) so that you can convert more measurements?
Sorry but I am a RAW beginner (But I am having fun :) ).

dr voodoo - August 29, 2003 06:45 PM (GMT)
There are several methodes:
CODE
main()
{
while(1==1)
{
system("Pause");
break;//If you do not put this here we'll go back to the top
}
}

or
CODE
main()
{
start:
system("Pause");
goto start;//If we put this here we will go to the top
}



Dixon - August 29, 2003 07:56 PM (GMT)
Thanks, I will look those over. But I already got it :) Still I will look at yours and mine and see which one works best.

TheHawgMaster - August 29, 2003 10:45 PM (GMT)
As long as you do not plan to restart the program hundreds of times you should be able to get away with something like the following:
CODE
void main()
{
   // Your main code here ...

   if(WeShouldRestart) {
       // De-initialize application here ...
       main();
   } // end if
} // end main
Of course it would be WinMain in a Win32 application but you should get the idea...

Geo - October 5, 2003 05:15 PM (GMT)
QUOTE (dr voodoo @ Aug 29 2003, 06:45 PM)
There are several methodes:
CODE
main()
{
start:
system("Pause");
goto start;//If we put this here we will go to the top
}

I'm a beginner too, but I was told not to use goto since it's not "correct programming". Anyway, just pointing it out, but it's an alternative for what you're looking.

dr voodoo - October 5, 2003 07:36 PM (GMT)
QUOTE
I'm a beginner too, but I was told not to use goto since it's not "correct programming". Anyway, just pointing it out, but it's an alternative for what you're looking.

Well many books tell you that goto is poor programing style. This is partly correct and wrong.

Correct because if you use object orientated programing goto can cause serious problems of destructors that are not called when leaving the scope.

Wrong if you use procedural programing because it can speed up applications and make code a lot more readble (but also less!). goto is much nearer to mashine code than while or for.

TheHawgMaster - October 5, 2003 08:20 PM (GMT)
It depends, as long as your algorithm is producural in nature, goto is often very helpful if used prudently. But in most general programming situations it will simply make your code harder to read and more error-prone than a for or while loop that represents the same thing.




* Hosted for free by InvisionFree