Thread: problem in goto command

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    24

    problem in goto command

    I wanted to display "www.iloveyou.com" using the following command but it shows "ilove.comyou" . I don't understand what is the problem.

    Code:
    int number;
    printf ("www.");
    goto x;
    z:
    printf (".com");
    goto y;
    x:
    printf ("ilove");
    goto z;
    y:
    printf ("you");
    getch ();
    return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    How about
    printf("www.iloveyou.com");
    and forgetting about silly tricks in which you have got a simple order of placement wrong
    ?
    :P

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why are you even doing this? The goto command should be used sparingly, because it is only rarely the best way to do something.

    What you've done is highlight the big problem with goto. It can lead to what is known as "spaghetti code": code that jumps from place to place in a disordered way and is hard to understand or maintain.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    24
    @manasij

    i want to learn "goto function"........

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by learning_grc View Post
    i want to learn "goto function"........
    Then you've to
    goto here ;

  6. #6
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I simply don't have the patience to go over ALL the reasons why goto is horrible. Mostly because of terribly disorganized code? (insert Anti-Goto Order banner here)

    also. It's displaying that because you're telling it to. Switch goto y; and goto z;

    Quote Originally Posted by TheBigH View Post
    What you've done is highlight the big problem with goto. It can lead to what is known as "spaghetti code": code that jumps from place to place in a disordered way and is hard to understand or maintain.
    nvm. ^ i've been beaten
    Last edited by Rodaxoleaux; 10-02-2011 at 03:31 PM. Reason: beat like a noob in CoD

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by learning_grc View Post
    I wanted to display "www.iloveyou.com" using the following command but it shows "ilove.comyou" . I don't understand what is the problem.
    Reorder the code in the order that it happens:

    Code:
    int number;
    printf ("www.");
    goto x;
    x:
    printf ("ilove");
    goto z;
    z:
    printf (".com");
    goto y;
    y:
    printf ("you");
    getch ();
    return 0;
    }
    There you go. See how easy that was?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read this to learn why goto is not a good idea for general use: Goto Considered Harmful. It does have it's place, but your code is not it.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    It does have it's place, but your code is not it.
    Actually it's a pretty good example of what you want them to learn about goto:
    Quote Originally Posted by TheBigH View Post
    What you've done is highlight the big problem with goto. It can lead to what is known as "spaghetti code": code that jumps from place to place in a disordered way and is hard to understand or maintain.
    If he has a hard time sorting out that small example, then he should get the idea that it's a pain to use, and avoid it. So as far as learning experiences go, it's pretty good.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by learning_grc View Post
    @manasij

    i want to learn "goto function"........
    "goto" is not a function.

    Diagnosis: The problem is that you want to learn about "goto".
    Solution: Do not learn about "goto". The less you know, the better!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by learning_grc View Post
    i want to learn "goto function"........
    Well, I would like you to learn how to program; but we can't always get what we ask for.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    They really should just make break and continue work with labeled loops. Then there would be no legitimate use for goto at all.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    "goto" is not a function.

    Diagnosis: The problem is that you want to learn about "goto".
    Solution: Do not learn about "goto". The less you know, the better!
    I would down mod this if I could.

    You should know about goto. You should know when to use it, and how it works. Not knowing about it isn't going to help you when you run into it.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    They really should just make break and continue work with labeled loops. Then there would be no legitimate use for goto at all.
    The "mimic exception handler" error handling style using goto would not see goto suitably replaced by such enhanced break and continue statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by King Mir View Post
    They really should just make break and continue work with labeled loops. Then there would be no legitimate use for goto at all.
    Well, my personal belief is that, based on the conceptual design for the C language, goto's should only be used for error handling; a concept which was resolved in C++ with try and catch.

    Quote Originally Posted by quzah View Post
    I would down mod this if I could.

    You should know about goto. You should know when to use it, and how it works. Not knowing about it isn't going to help you when you run into it.
    Quzah.
    You should learn how it works, nothing really more. Are we really going to turn this into another debate about 'goto'? This has been done so many times before, and (most importantly), has never helped the OP.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chmod command problem
    By JFonseka in forum C Programming
    Replies: 22
    Last Post: 10-22-2007, 07:09 AM
  2. Problem with goto
    By Tropicalia in forum C++ Programming
    Replies: 36
    Last Post: 09-30-2006, 07:33 PM
  3. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  4. Goto Command
    By swgh in forum C++ Programming
    Replies: 13
    Last Post: 04-30-2005, 05:24 PM
  5. problem with switch command
    By DoItAllMom115 in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2003, 04:33 PM