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
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
You don't. Period.how do you use it?
agreed.Originally Posted by 7stud
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
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.
This code manages to exit the inner most for loop , ieCode: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 }but the middle one i.e.Code:for(i=1;i<=101;i++)does not stop.Code:for(array_position=1;array_position<=11;array_position++)
Any ideas on how to stop the second loop, while leaving the first one,i.e.running.Code:for ( horizontal_position = 0; horizontal_position <=width;horizontal_position = horizontal_position + step_size)
TIA
Last edited by a1pro; 04-29-2005 at 04:10 AM.
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?
never mind
i got it working now
inner most loop is a do-while loop now
Sorry for hijacking, but is there a replacement for goto loops?
Basically, goto end; goes to the part where it asks to do another calculation. Is there anyway to achieve this without 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(); }
yes but it may well require you rethink the structure
I noticed that everyone says "Don't use Gotos!" but why? Why are they bad?!?!
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.
GOTOs jump around in your code, are confusing to deal with, and when you need to change something it can be very difficult.Originally Posted by Cool-August
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?
He didn't ask for reasons not to use it, yes, goto is shunned but it has its uses, particularly for optimisations.
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.