Well one way would be to use the goto statement. Pretty much, you type any word, then ":" after it. Then, you can later go to it.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void Function( ){
beginning:
/* Code
----- code -----
----- code -----
*/
// Before ending the function
std::cout<<"Are you sure you want to quit? (y/n?) \n"; // Prompt User
std::cin>>input; // Get Answer
if(input == "n") goto beginning; // This will go back to line 2, restarting the function.
else return;
}
|
Another way, of course, is just to use recursion. Pretty much, in the if statement on line 12 (my example above), replace
if(input == "y") goto beginning;
with
if(input == "y") Function();
What this does is just call the function inside the function, hence the name "recursion".
You can always just use a while loop, but I usually use these. It is just easier (to me) to just do this than a while loop.
AFAIK, there are no catches to these techniques, or in other words, there are no consequences. If there are, then I need to rethink my answer...
If you want a reference to the
goto statement, here you go:
http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx