Thread: Goto Command

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Goto Command

    I have heard that you can use the GOTO command in C++ as I did in BASIC. is this true, and also, how do you use it? Do Ii have to declare the command at the start of the code in
    int main()?

    Any help would be appriciated

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    how do you use it?
    You don't. Period.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by 7stud
    You don't. Period.
    agreed.

    but here: http://www.cppreference.com/keywords/goto.html

    again, DO NOT USE GOTO.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    technically you can use it
    http://www.sscnet.ucla.edu/geog/gessler/borland/cpp.htm

    Anyway.

    A related question since goto statements are a no no in c++, how do you exit a nested loop?

    I've got 4 nested for loops( actually i've only got 3 atm, but i'm trying to get the program working along the x axis first), basically the first two sweep the x and y coordinates and the other 2 do the checking, so how do I exit the first two loops while leaving the other two running.

    Code:
    
     for ( horizontal_position = 0; horizontal_position <=width;horizontal_position = horizontal_position + step_size)
            {
              counter=0;  //intializing counter value for each postion
              steps=0;
              
              motion(horizontal_position,carrier_position_x);// carrier position in 1 ps steps
              steps++;        
                       
             for(array_position=1;array_position<=11;array_position++)// check for all lattice sites
             {
              
              for(i=1;i<=101;i++)// check all positions the carrier will be in
              {
                               
               vector[1][1]=lattice_points[array_position][1]-carrier_position_x[i];//x position 
               cout<<vector[1][1]<<" "<<lattice_points[array_position][1]<<" "<<carrier_position_x[i]<<" "<<array_position<<" "<<horizontal_position<<endl;
               
               vector[2][1]=lattice_points[array_position][2]-vertical_position;//y position
             
               modulus=sqrt(pow(vector[1][1],2)+pow(vector[2][1],2));
             
             //cout<<vector[1][1]<<" "<<vector[2][1]<<" "<<modulus<<" "<<counter<<" "<<steps<<endl;
               
               if (modulus<radius)
               {
                counter++; 
                 break;   
                    
               }
                            
               
              }//for loop finish checking the trajectory of the carrier           
             }// for loop finish checking all lattice sites 
            
            }
    This code manages to exit the inner most for loop , ie
    Code:
    for(i=1;i<=101;i++)
    but the middle one i.e.
    Code:
    for(array_position=1;array_position<=11;array_position++)
    does not stop.

    Any ideas on how to stop the second loop, while leaving the first one,i.e.
    Code:
    for ( horizontal_position = 0; horizontal_position <=width;horizontal_position = horizontal_position + step_size)
    running.

    TIA
    Last edited by a1pro; 04-29-2005 at 04:10 AM.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    There is indeed a goto in C++ just like in basic. As 7stud was saying most people tend to look down on goto because it leads to bad style and can make it hard to see what is going on in your code. Others say goto has its advantages to break out of deeply nested loops.

    So my advice for you is to stay away from goto until you are more experienced and than would know when it could be useful for you and use functions instead of goto.
    Woop?

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    never mind
    i got it working now

    inner most loop is a do-while loop now

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Sorry for hijacking, but is there a replacement for goto loops?

    Code:
          if (opt == "1")
          {
                  long int a = 1;
                  char* newchar;
                  int z;
                  cout<<"Please input integer: ";
                  cin>>newchar;
                  z = atoi(newchar);  
                  cin.ignore();  
                            
                  /*while (!(cin>>z))
                  {
                        cout<<"Please do not input alphabets.\n\n";
                        cin.clear();
                        cin.ignore(std::numeric_limits < int >::max(), '\n');
                        goto end;
                  }*/
    
                  if (z > 1000000)
                  {
                        cout<<"Please do not enter numbers larger than 1,000,000.  \n\n";
                       // cin.get();
                        goto end;    
                  } 
                  cout<<"\nConfirm? (y/n): ";
                  cin>>dozza;
                  cin.get();
                  //getline(cin, dozza, '\n');
                  if ((dozza == "y") || (dozza == "Y"))
                  {
                           b = 1;
                           z = z*80899;
                           _ftime(&sb);
                           start = sb.time*1000 + sb.millitm;
                           while (b <= z)
                           {
                                   bench1(a);
                                   b++;
                           }
                           _ftime(&eb);
                           end = eb.time*1000 + eb.millitm;
                           float x = (end - start);
                           float y = x/1000;
                           cout << "Benchmark took " << y << " seconds\n\n";
                           cout<<"Export results? (y/n): ";
                           cin>>exp;
                           if (exp == "y")
                           {
                                        time(&timz);
                                        ofstream exp("export.txt", ios::app);
                                        std::string thetime = ctime(&timz);
                                        thetime.erase(thetime.find('\n', 0), 1);
                                        exp<<"("<<thetime<<") Integer Benchmark: "<<y<<" seconds.\n";
                                        exp.close();
                                        cout<<"Exported results successfully. Please see export.txt.\n\n";
                           }
                           cin.get();
                  }
    Basically, goto end; goes to the part where it asks to do another calculation. Is there anyway to achieve this without goto loops?

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    yes but it may well require you rethink the structure

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I noticed that everyone says "Don't use Gotos!" but why? Why are they bad?!?!

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read the faq

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Break deeply nested loops into separate functions.

    Use a flag to break out of necessary nested loops.

    Using goto is acceptable if you are a competent, experienced programmer with specific reasons for it. Unfortunately, beginners like to use it as an "easy" way out and write code that is hard to read, understand and maintain. If you are asking C++ questions on this site, I can almost guarantee that you should not be using goto.

  12. #12
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Quote Originally Posted by Cool-August
    I noticed that everyone says "Don't use Gotos!" but why? Why are they bad?!?!
    GOTOs jump around in your code, are confusing to deal with, and when you need to change something it can be very difficult.

    Functions are better because they can use parameters, return specific values (side effects are usu. intentional & obvious), and use local variables.


    Plus they just keep your code all in one spot and better structured.
    Right?

  13. #13
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    He didn't ask for reasons not to use it, yes, goto is shunned but it has its uses, particularly for optimisations.

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I doubt that you'll optimize much/any using goto. Modern compilers are pretty good at keeping code rather efficient.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM