-
Help with a goto
First off, I know goto's are generally evil because of the "spaghetti code" they tend to create, but there's no other way I can see to do what I need to do.
Here's the problem. I have a run function in a class. I have a goto marker called end: at the... well, at the end of it. I would like to go to end from a different function in the same class, but I keep getting errors.
Code:
appclass.cpp 309 error: label 'end' used, but not defined
These errors are in the functions where I'm trying to call the goto, which are after the run function. Is it possible to go to a label point in a different function with gotos?
-
No, you can't goto between functions.
-
Put the stuff after the "end" label into a function of it's own. And then call it at the appropriate points in both your other functions that need it.
Without code or further rationale about what you're trying to achieve, it's harder to be more specific.
-
thanks guys. That helped me get it settled. I made the functions that I was trying to jump out of return an integer, and if the integer was 1 or 2, it went to the end. I'll post all the code for this game in a few days time, so you guys can root through it and let me know what's good and what's bad. Thanks!
-
> I'll post all the code for this game in a few days time, so you guys can root through it and let me know what's good and what's bad. Thanks!
Thanks. You know what one of the suggestion will be, don't you? Get rid of that goto :D
-
Well, they're good for getting out of a nested set of loops. I've never heard of any other justifiable uses for them, though.
-
The other ones are for common exit code and breaking a loop from a switch.
-
Coroutines, not natively supported by C, can be implemented using goto, among other mechanisms.