C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 6th, 2006, 11:07 PM
Douglas.Mellon Douglas.Mellon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 Douglas.Mellon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 5 sec
Reputation Power: 0
Exclamation Return to first line...

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

Reply With Quote
  #2  
Old April 7th, 2006, 01:57 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
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.

Reply With Quote
  #3  
Old April 7th, 2006, 06:49 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 996 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2Folding Points: 878024 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 11 m 36 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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.
Comments on this post
Geo.Garnett agrees: Hey Buddie. Long Time no postie =)&gt;
__________________
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

Reply With Quote
  #4  
Old April 10th, 2006, 09:57 PM
Douglas.Mellon Douglas.Mellon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 Douglas.Mellon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 5 sec
Reputation Power: 0
thanks alot for the help... I will try these out.

Reply With Quote
  #5  
Old April 15th, 2006, 10:58 PM
Douglas.Mellon Douglas.Mellon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 Douglas.Mellon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 5 sec
Reputation Power: 0
Thanks guys... i got it working.
~ Doug

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Return to first line...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT