I`ve tried the tutorials, but i couldn`t find anything about goto. I don`t know how to set labels and i don`t know how to use goto. Examples would really help.
I`ve tried the tutorials, but i couldn`t find anything about goto. I don`t know how to set labels and i don`t know how to use goto. Examples would really help.
why do you want to use goto? there really is not a very big use for it. A ussally better solution for goto is a function call
Woop?
Well, i don`t really know what you`re talking about when you talk about 'function calls' but i`m currently working on a game-like thing, for which i think i need goto. But how do I set labels? And do i need to useorCode:goto(label);or something else?Code:goto label;
Last edited by darealnash; 07-05-2004 at 01:43 AM. Reason: i made a mistake
here's is how a function call would work
Code:#include <iostream> using namespace std; int main() { void CallFunction(); cout<<"Calling Function"; CallFunction(); return 0; } void CallFunction() { cout<<"Function Called"; }
Woop?
Do function calls make the program go to a certain part of the code (similar to goto) or something else? Because i need to repeat a certain part of the code after a certain input.
oh for that you would use a loop eg.
Code:int input = 0; while(input==0) { cout<<"hey input still equals 0"; cout<<"What do you want input to be?"; cin>>input; }
Woop?
I don`t think thats what i mean, but here`s my code:
Do you know what i`m doing wrong here?Code:#include <iostream.h> int main() { int input; cout<<"This game simulates a rich life."<<endl; 1 cout<<"Input menu function"<<endl; cout<<"1: Spend money."<<endl; cout<<"2: Play a video game."<<endl; cout<<"3: Drive around in a car"<<endl; cout<<"4: Climb Mount Everest"<<endl; cout<<"5: Fly around in a helicopter"<<endl; cout<<"6: Kill yourself"<<endl; cin>>input; 3 if (input==1) { cout<<"You buy a helicopter, a building, and a private island."<<endl; goto 1; } else if (input==2) { cout<<"You turn on your Zcube and play for half an hour."<<endl; goto 1; } else if (input==3) { cout<<"You drive around in your $900,000,000.95 limo."<<endl; goto 1; } else if (input==4) { int action; cout<<"You climb Mount everest. Now what?"<<endl; 2 cout<<"1: Shout:'I`m the king of the world!!!'"<<endl; cout<<"2: Go back down"<<endl; cin>>action; if (action==1) { cout<<"I`m the king of the world!!!"<<endl; goto 2; } else if (action==2) { goto 1; } else { cout<<"Please input menu functions only"<<endl; goto 2; } } else if (input==5) { cout<<"You fly around in your helicopter."<<endl; goto 1; } else if (input==6) { goto 3; return 0; } }
you want to do this instead its much cleaner and easier to read
Code:#include <iostream> using namespace std; void menu();//Function Protypes void mountain(); int main() { menu(); return 0; } void menu() { int input; cout<<"This game simulates a rich life."<<endl; cout<<"Input menu function"<<endl; cout<<"1: Spend money."<<endl; cout<<"2: Play a video game."<<endl; cout<<"3: Drive around in a car"<<endl; cout<<"4: Climb Mount Everest"<<endl; cout<<"5: Fly around in a helicopter"<<endl; cout<<"6: Kill yourself"<<endl; cin>>input; switch(input) { case 1: cout<<"You buy a helicopter, a building, and a private island."<<endl; menu();//Recursively calling menu break;//Keeps One case from going into the other case 2: cout<<"You turn on your Zcube and play for half an hour."<<endl; menu(); break; case 3: cout<<"You drive around in your $900,000,000.95 limo."<<endl; menu(); break; case 4: {//have to use braces when declaring a varible int action; cout<<"You climb Mount everest. Now what?"<<endl; cout<<"1: Shout:'I`m the king of the world!!!'"<<endl; cout<<"2: Go back down"<<endl; cin>>action; if(action==1) { mountain(); } if(action==2) { menu(); } break; } case 5: cout<<"You fly around in your helicopter."<<endl; menu(); break; case 6: cout<<"You Killed Yourself"<<endl; break; } } void mountain() { cout<<"I`m the king of the world!!!"<<endl; cout<<"Now what?"<<endl; cout<<"1: Shout agian"<<endl; cout<<"2: Go back down"<<endl; int mAction; cin>>mAction; if(action==1) { mountian(); } if(action==2) { menu(); } }
Woop?
"untitled1.cpp": E2451 Undefined symbol 'action' in function mountain() at line 70
"untitled1.cpp": E2268 Call to undefined function 'mountian' in function mountain() at line 72
"untitled1.cpp": E2134 Compound statement missing } in function mountain() at line 77
sorry change the action to mAction
Code:void mountain() { cout<<"I`m the king of the world!!!"<<endl; int mAction; cout<<"Now what?"<<endl; cout<<"1: Shout agian"<<endl; cout<<"2: Go back down"<<endl; cin>>mAction; if(mAction==1) { mountain(); } if(mAction==2) { menu(); } }
Last edited by prog-bman; 07-05-2004 at 03:02 AM.
Woop?
"untitled1.cpp": E2268 Call to undefined function 'mountian' in function mountain() at line 72
"untitled1.cpp": E2134 Compound statement missing } in function mountain() at line 77
here's the full thing.
Code:#include <iostream> using namespace std; void menu();//Function Protypes void mountain(); int main() { menu(); return 0; } void menu() { int input; cout<<"This game simulates a rich life."<<endl; cout<<"Input menu function"<<endl; cout<<"1: Spend money."<<endl; cout<<"2: Play a video game."<<endl; cout<<"3: Drive around in a car"<<endl; cout<<"4: Climb Mount Everest"<<endl; cout<<"5: Fly around in a helicopter"<<endl; cout<<"6: Kill yourself"<<endl; cin>>input; switch(input) { case 1: cout<<"You buy a helicopter, a building, and a private island."<<endl; menu();//Recursively calling menu break;//Keeps One case from going into the other case 2: cout<<"You turn on your Zcube and play for half an hour."<<endl; menu(); break; case 3: cout<<"You drive around in your $900,000,000.95 limo."<<endl; menu(); break; case 4: {//have to use braces when declaring a varible int action; cout<<"You climb Mount everest. Now what?"<<endl; cout<<"1: Shout:'I`m the king of the world!!!'"<<endl; cout<<"2: Go back down"<<endl; cin>>action; if(action==1) { mountain(); } if(action==2) { menu(); } break; } case 5: cout<<"You fly around in your helicopter."<<endl; menu(); break; case 6: cout<<"You Killed Yourself"<<endl; break; } } void mountain() { cout<<"I`m the king of the world!!!"<<endl; int mAction; cout<<"Now what?"<<endl; cout<<"1: Shout agian"<<endl; cout<<"2: Go back down"<<endl; cin>>mAction; if(mAction==1) { mountain(); } if(mAction==2) { menu(); } }
Woop?
Thanks!!!
There is no reason to call menu recursivly.
As for the syntax for goto it is simply this:Code:void menu() { int input; bool keepalive = true; do { cout<<"This game simulates a rich life."<<endl; cout<<"Input menu function"<<endl; cout<<"1: Spend money."<<endl; cout<<"2: Play a video game."<<endl; cout<<"3: Drive around in a car"<<endl; cout<<"4: Climb Mount Everest"<<endl; cout<<"5: Fly around in a helicopter"<<endl; cout<<"6: Kill yourself"<<endl; cin>>input; switch(input) { case 1: cout<<"You buy a helicopter, a building, and a private island."<<endl; break;//Keeps One case from going into the other case 2: cout<<"You turn on your Zcube and play for half an hour."<<endl; break; case 3: cout<<"You drive around in your $900,000,000.95 limo."<<endl; break; case 4: {//have to use braces when declaring a varible int action; cout<<"You climb Mount everest. Now what?"<<endl; cout<<"1: Shout:'I`m the king of the world!!!'"<<endl; cout<<"2: Go back down"<<endl; cin>>action; if(action==1) mountain(); break; } case 5: cout<<"You fly around in your helicopter."<<endl; break; case 6: cout<<"You Killed Yourself"<<endl; keepalive = false; break; } }while(keepalive}; }
Code:goto_label: goto goto_label;
I find it often helpful to make my entire program just one huge main function, with all my variables global, and then just use goto jumps to get where I need. The benifit of this and the globals, is that on the off chance main gets too big, I can always just make one more big function, and all the variables are accessable to it. This lends greatly to the overall readability of the program.
Quzah.
Hope is the first step on the road to disappointment.