ERROR: The label "foo" is used but not defined

So, I cheated on some C ++ and got the previously reported error with some code that looked something like this:

#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 complicated, but it's just to show you the error you received.

Now I understand that for some reason everyone despises the goto statement, but my actual code is full of so many gotos that I really don't have the time and patience to go back and change them all. Besides, Iโ€™m like a novice programmer, and I think gotos and shortcuts are very easy to use.

My question is, how can I predefine these labels so that the fruit () function knows what it is? In addition, I need to do this without moving labels from the main function.

+4
source share
4 answers

The goto operator can only work with locally defined labels; it cannot go to other functions.

Thus, labels in main will not be referenced, and goto in fruit will not find labels.

+7
source

What you are trying to do - jump between functions - is not valid for a whole host of reasons, not least being an area of โ€‹โ€‹objects and a lifetime, consider:

 void foo() { if(feelLikeIt) goto foobar; } void bar() { std::string message = "Hello"; foobar: cout << message << endl; } 

Jumping to foobar from foo is illegal because the "message" will not exist.

Thus, the language simply does not allow you to do this.

Also, the way you are trying to use "goto" will not allow you to reuse the "fruit ()" function, because it always decides what to do with the choice and not the calling function. What if you want to do this:

 cout<<"What do you want to eat? (a/b)"; fruit(); foo: cout<<"You eat an apple."; fooo: cout<<"You eat a banana."; cout<<"What does your friend want to eat? (a/b)"; fruit(); // oops, you just created a loop because fruit made the decision on what to do next. 

What you STRONGLY want to do is use the "fruit ()" function as a function that returns a value.

 enum Fruit { NoSuchFruit, Apple, Banana }; Fruit fruit(const char* request) { char foodstuffs; cout << request << " (a/b)"; cin >> foodstuffs; switch (foodstuffs) { case 'a': return Apple; case 'b': return Banana; default: cout << "Don't recognize that fruit (" << foodstuffs << ")." << endl; return NoSuchFruit; } } const char* fruitNames[] = { "nothing", "an apple" ,"a banana" }; int main() { Fruit eaten = fruit("What do you want to eat?"); cout << "You ate " << fruitNames[eaten] << "." << endl; eaten = fruit("What does your friend want to eat?"); cout << "Your friend ate " << fruitNames[eaten] << "." << endl; } 
+4
source

Unfortunately. You cannot goto on a label that is outside the current executable function. In addition, there are other restrictions on the use of goto . For example, you cannot skip a variable definition with goto . And there are others that I donโ€™t quite understand about.

In the bottom line?

Do not use goto .

+1
source

goto cannot be used to navigate outside the current function. Try to return something from the function and use this in the else clause.

0
source

Source: https://habr.com/ru/post/1483735/


All Articles