Restarting a Program

I am creating a program but am unsure how to restart it after it is completed? I can use a loop function or a do loop.

I was thinking of asking the question

"Do you wish to play again" Yes/No

If answered yes it restarts the program if no it ends.

What is the correct way to code this? I been looking at other codes that people showed and I can not seem to get them to work.
Something like this, then?
1
2
3
4
5
std::string str;
do {
    std::cout<<"Type 'y' to go again, anything else to quit."<<std::endl;
    std::getline(std::cin, str);
} while(str != "y");
its better to just do it like Zhuge suggested and put it in a do-while loop, but you can also make the main function recursive like so:
1
2
3
4
5
6
char continue;
cin >> continue;
if(continue == 'y')
	main()
else
	exit(1)


once again i really dont recommend doing it this way, but you can :)
Last edited on
^No no no...NEVER do that. That's just asking for a stack overflow...
@ascii: What if the user keeps choosing to repeat the program? Eventually the stack will be exhausted by all the recursive called, as firedraco says. You also may want to use a different name for your variable, as continue is a keyword and not a valid symbol name.
:o didnt think about the continue thing, i just wrote that into the post directly so i didnt think about keywords. as for the stack overflow, i did say "i dont recommend doing it this way" :)
*throws up all over the ground*

Even a goto statement would have been cleaner, anything other than making main recursive.

I think there's some blood in the puke, should probably see a doctor about that.
Thank you for being an ignominious jerk and reiterating what the two people above you said for the sake of being rude in a feeble attempt to make me feel worse about something that I already said I didn't recommend doing and was wrong. You're rather disgusting, both for the vomit and the post.
Topic archived. No new replies allowed.