Thread: Help with a goto

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, you can't goto between functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    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!

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > 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
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The other ones are for common exit code and breaking a loop from a switch.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Coroutines, not natively supported by C, can be implemented using goto, among other mechanisms.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM