I am only new and learning and am trying to use a GOTO functiong in my code but dont know how to create the label that GOTO is meant to...well go to.
any help is as allways appreciated.
Printable View
I am only new and learning and am trying to use a GOTO functiong in my code but dont know how to create the label that GOTO is meant to...well go to.
any help is as allways appreciated.
with "label:", as in
Code:start:
//do something
middle:
//something else
end:
return 0;
when i put that form of code in the following form:
this does not work the error says label top is used but not defined.Code:int main (void)
{
top:
//....
}
int second (void)
{
//...
if //function
GOTO top
Depends, what do you want to do?
I'm just a beginner in C++ but i don't think jumping out of functions with a goto is a good idea
do you know how to jump from a function to a previous function depending on a variable such as:
basing it that this code is in a second function and i want to jump back to the main function.Code:if answer = yes
GOTO //jump back to start of main function
else EXIT
Most IDEs will bolden keywords.Code:// The keyword is
goto
// not
GOTO
yeah in my actual source code i dont have it capitalized....
Well then pasting something that isn't your source code and asking why your source code is getting errors doesn't make much sense, does it.
...and labels, like variables have scope. Which is why this:
doesn't work.Code:void func();
int main ()
{
top:
func();
}
void func()
{
goto top;
}
ok i get wat your saying and yes it was stupid of me but what im saying is how do i jump out of a second function and back into the beggining of the first?
or is it possible to make the application start over completly based on results from
Code:if answer = yes
restart program
else exit program
You would base your goto off the return of the function.
Code:#include <iostream>
int func(int);
int main ()
{
int x = 0;
top:
x = func(x);
if (x == 1)
goto top;
return 0;
}
int func(int var1)
{
if (var1 == 0) {
std::cout << "Going to top\n";
return 1;
}
std::cout << "Not going to top";
return 0;
}
Even if labels didn't have scope and you could jump into another function it would cause problems with the stack. The stack is set up with local function variables, arguments and the return address of the function you are in, if you jumped into another function these would all be incorrect.
You don't need goto! Your example could be coded like this
Code:int main (void)
{
bool bExit=false;
while (!bExit)
bExit=Second();
}
bool Second (void)
{
...
//if answer=="yes";
return false;//don't exit
else
return true;//do exit
k thnx
goto's r good,i just love goto's,goto's give your code just the chic look u always wanted in it,and if u have mastered goto,come to OOP,thats the latest trend,make everything an object,do some fanciful interfaces,now u r realizing art.
__________________________________________________ __
"its not programming if its not oop"
wouldnt it make the code slightly more complicated if everything was made an object and every movement was defined by goto???
You can use a stronger goto in the form of setjmp and longjmp, but that's ugly and awkward. A better option is to throw an exception. Both of those will safely unwind the function call stack and take you back to where you want to be.Quote:
do you know how to jump from a function to a previous function depending on a variable
But those are for exceptional situations, ie. errors and unexpected stuff. For normal operation, the return value of a function that drives a loop is more intuitive. :)
Yes. :)Quote:
wouldnt it make the code slightly more complicated if everything was made an object
Not necessarily. That's the way assembly works and I find it easier to read and write than C++. I guess it depends on the idioms that a language best supports that determine how complicated unconditional jumps are.Quote:
and every movement was defined by goto???