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.
Printable View
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
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;
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";
}
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;
}
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();
}
}
"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();
}
}
"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();
}
}
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.
Good advice, as always, quzah. The glue that's required to couple together functions and modules just makes your program bigger as well. If you use as few functions and classes as possible, you can spend less time and space on putting things together and more on the meat of the program. That's where real productivity comes from. :)Quote:
Originally Posted by quzah
These last two post hurt my brain, really.
That kinda kills the point of OOP and lower memory usage for variables not often used?Quote:
Originally Posted by quzah
~Meloshski
quzah was been sarcastic
he was? :eek: crap, i just spent the last 6 hours rewritting all of my code in main with goto's and globals. :mad:Quote:
Originally Posted by Thantos
Hell no, it's definately in the spirit of OOP!Quote:
Originally Posted by Meloshski
main has a bunch of stuff in it.
main is a huge function.
After all, OOP is all about "has a" / "is a" relationships!
;)
Quzah.
:D :D :D
I bout died when I read Quzah's post. And he didn't even say if he was joking or not. Later I find out that my suspicions were accurate....her was joking. Thank goodness.
You guys kill me.
And to the poster I have some really good advice on goto. Avoid it like the plague and pretend that it doesn't exist.
Anything that can be done with goto can be accomplished with much more finesse using other C/C++ constructs and logic.
Goto. Danger Will Robinson,....Danger!!!
i used goto.... ONCE. It put a nice little bug in the middle of my program. Later in my C++ bible i learnd, like the last replier "to avoid it like a plague." Not only is it unnecessary but it can produce bugs. Quzah when i saw ur post, i wondered how anyone could say such a thing, i didnt know you were joking but i did know enuff to stay away from such advice.
tyler
Can someone tell me why you need braces around a case statement if it has a variable declaration in it? I did not know that because I never declare variables there but I assumed it just went right down to the end of the switch structure. I will have to try it out when I get home.