ERROR: Label "foo" was referenced but not defined.

So I was fooling around with some C++ and got the previously stated error with some code that looked kind of like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

char foodstuffs;

void fruit()
{
cin>>foodstuffs;
switch(foodstuffs)
{
case 'a': goto foo; break;
case 'b': goto fooo; break;
}
}

int main()
{
cout<<"What do you want to eat? (a/b)";
fruit();
foo: cout<<"You eat an apple.";
fooo: cout<<"You eat a banana.";
}


The exact code was much more complex, but this is just to show you the error I got.

Now I realize that everyone here despises the "goto" statement for some reason, but my actual code is full of so many gotos that I don't really have the time/patience to go back and change them all. Also, I'm kind of a novice programmer and I find using gotos and labels very easy.

My question is how can I predefine these labels so that the function fruit() knows what they are? Also, I need to do this without moving the labels out of the main function.
Just from a programming perspective, you shouldn't be using goto to jump to a different function as that seems to be asking to screw up the stack. So I'd think this is impossible.

You might be able to get away with long jumps or whatever the non-local jump function is but I would highly recommend simply investing the time into breaking that bad habit of using gotos/labels because they are 'easy'. You will find this becomes quickly unmanageable with code of any interesting complexity.

http://www.cplusplus.com/reference/csetjmp/
strong no to Goto, dont get accustomed to using go to statements, they are really bad for various reasons!!!
strong no to Goto, dont get accustomed to using go to statements, they are really bad for various reasons!!!

If you're going to completely ignore my problem and rant about goto statements, at least tell me WHY they're bad.
Well, mainly because they're a complete jump to a different section of the program, disregarding any already-called function paths, which can cause a tremendous amount of issues, such as calling functions twice that create dynamic variables and causing a memory leak, or being mentally hard to follow. That, and there is almost always a way to avoid using them in the first place that isn't that complicated. Like here in your code, make fruit return a boolean value, and have either foo run if it returns true, and fooo run if it returns false.

Generally, it is also bad to use goto statements because they do not respect function parameters or loops- it is a complete jump, regardless of where, to the location specified. This can easily screw up loops. At the same time, it causes several issues when trying to be used in two different functions- such as in your case. Specifically, it doesn't work at all, because the "foo" and "fooo" cases for the loop are only defined in main. They're a bit like... local variables, except in this case they're local jump-points. Hence why your code doesn't work, nor would ever- you can't make global goto destinations for obvious reasons, and functions can never be aware of any of these locations that aren't inside of themselves- another reason why using them is bad practice.
you can't use goto to jump from one function to the next. You can only use it to move within a function.
If you're going to completely ignore my problem and rant about goto statements

It wasn't a rant, it was entirely sensible and well-meaning advice. goto's are terrible, and getting used to them is a truly terrible idea if you ever want to be more than a terrible programmer whose code doesn't work properly, you should make the effort to stop using them and learn how to structure yuour code to make it easier for you to develop and maintain, and more reliable for others to use.

I mean, seriously, if everybody keeps telling you that goto's are bad, at what point do you start to think that, maybe, they might be bad?

If you're genuinely interested in learning why they're bad, then typing "why is goto bad" into Google will, no doubt, provide you with some food for thought.

In case this is not noticed between the rants, @Yanson is correct, goto cannot leave a function (only return, throw, and longjmp can)
one more reason why goto is bad:
one of the most important principles in programming is readability, both your ability to read the code and any other programmer's ability to read your code.
using goto impressively drains readability from any code, you might not notice it in such tiny program, but once you get more than 100 lines of code, you will be confused about the path that your program is gonna take.

and again, please don't underestimate readability, it's one of the most important principles in programming.

that means, you should've indented your code.
> If you're going to completely ignore my problem and rant about goto statements,
> at least tell me WHY they're bad.

There is a somewhat drawn out discussion about the use and abuse of goto (with participants arguing on both sides) starting here:
http://www.cplusplus.com/forum/general/93381/#msg501742


> I mean, seriously, if everybody keeps telling you that goto's are bad,
> at what point do you start to think that, maybe, they might be bad?

At the point when you have reasoned it out for yourself and reached a cogent conclusion. When you do not just 'think' that excessive use of goto is bad, but you know why it is bad. Not till then. When you would be able to say something more than: "goto's are terrible, and getting used to them is a truly terrible idea if you ever want to be more than a terrible programmer you should make the effort to stop using them". When you can say why unbridled use of goto is bad.

Hint: A whole lot of people swear that the use of goto anywhere is bad. The C++ community as a whole had three opportunities to say: "we know that the use goto anywhere is unconditionally bad; we keep it only for compatibility with legacy code. It is deprecated" - once in 1998, again in 2003, and recently in 2013; but it did not. Clearly there is more to it than blind unquestioning belief; there is something worth thinking about.

You don't become a good programmer by subscribing to religious beliefs; you become one by learning to think for yourselves.
Anybody with access to this forum has access to the Internet at large, which means access to the greatest, and easiest to access, source of information the world has ever known. It is trivially easy for anyone who has heard that there might be reasons not to use goto, to find out for themselves why. Asking people on a forum to type out again information that is already out there, serves no purpose whatsoever, except maybe a childish need for attention.

I don't for one moment advocate religious belief in anything, least of all in programming. What I do advocate is doing some research to understand what it is you're doing, and finding out what people who have more experience than oneself have discovered and learned - not whining petulantly that a bunch of strangers on the Internet aren't falling over themselves to act as unpaid personal tutors.

The ability to go away and research things you don't understand is vastly important in programming. I've been doing this for 20 years (in C++; longer if you include other languages), and I still have to do it on a regular basis. If a beginning programmer can't - or won't - learn do that, then they'll never get anywhere.

And, again, I have to ask - if one is an inexperienced programmer, and the overwhelming majority of more experienced developers give the same advice - then why would you not go and find out why they're giving you that advice?

EDIT: You'll note that I gave the OP some very clear and straightforward advice for how to quickly and easily find sources of information on the topic. I was doing the exact opposite of expecting someone to blindly follow my - or anyone else's - words religiously.
Last edited on
Topic archived. No new replies allowed.