| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
hello everyone,
I was wondering if there is a way to return to the first line of C++ code? So say the program executes and everything goes well then at the last line of code the programs starts over at line one. Any help would be greatly appreciated. Thanks in advance, Doug Mellon |
|
#2
|
||||
|
||||
|
Well there is a few ways to do this, you could put the program itself in a loop that exits when the user terminates it or you could just make a cin statement and use the goto function. The first is far more professional but both work fine.
While Loop ("Please excuse the simplicity its late") Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int a=0,i=0;
while (i != 1)
{
cout<<"Run the program\n";
cout<<"Would you like to exit or start over\n"
<<"0 = start over || 1 = exit\n";
cin>>i;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Using the goto statement. Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int a=0,i=0;
top:
cout<<"Run the program\n";
cout<<"Would you like to exit or start over\n"
<<"0 = start over || 1 = exit\n";
cin>>i;
if(i==0)
{
goto top;
}
else
{
system("PAUSE");
return 0;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Now none of these example have any error checking so beware =)
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
|
#3
|
||||
|
||||
|
if you're really sick, you could even simply call the main() function...
Doing this multiple times will fill up your memory pretty fast though, as it is recursive. The do-while loop is your best bet.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 278
![]() |
|
#4
|
|||
|
|||
|
thanks alot for the help... I will try these out.
|
|
#5
|
|||
|
|||
|
Thanks guys... i got it working.
~ Doug |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Return to first line... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|