Like Tree6Likes
  • 1 Post By Salem
  • 1 Post By c99tutorial
  • 1 Post By ledow
  • 3 Post By c99tutorial

How can I use "goto" for group of multiple statements?

This is a discussion on How can I use "goto" for group of multiple statements? within the C Programming forums, part of the General Programming Boards category; How can I use "goto" for group of multiple statements in the code as following: Code: title: { printf("______________\n") printf("The ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    How can I use "goto" for group of multiple statements?

    How can I use "goto" for group of multiple statements in the code as following:

    Code:
    title: 
    {
    printf("______________\n")
    printf("The Hollywood\n");
    printf("______________\n")
    }
    
    printf(.....)
    scanf(.....)                  ///other statements (main program)
    clrscr();
    
    goto title;
    In this code, I want to print the title at first and then use all function (between) in a program. At last I want to clear everything and reprint the title.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Just imagine, as a starting point for discussion, that - other than what you post - we have no idea what you are trying to achieve.

    Now, try reading your post with that in mind. Do you think it actually gives us any useful idea of what you are trying to do?

    If you think it does, you're wrong.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    ANY backwards goto is a sign that you really ought to be using a loop construct of some kind.
    dennis.cpp likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    At last I want to clear everything and reprint the title.
    That sounds more like a function than a goto. If you just want to print the title more than once then write a function called PrintTitle(). Or something similar.

    If you want to go back to the beginning of the program and repeat all the logic then look into what Salem suggested and decide if you want a loop that runs a fixed number of times (for loop) or a while loop that runs conditionally.

    I agree with grumpy though in the fact that your question is very broad and non-descript leaving it up to us to try and interpret both the question and the solution.

  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by shansajid View Post
    How can I use "goto" for group of multiple statements
    Don't.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,100
    Quote Originally Posted by shansajid View Post
    How can I use "goto" for group of multiple statements in the code as following
    You should think of a label as being bound to one statement rather than a group of statements. Try the following:

    Code:
    int main(void)
    {
        char buf[100] = "";
    title: 
        printf("______________\n");
        printf("The Hollywood\n");
        printf("______________\n");
         
        printf("Press q to quit: ");
        scanf("%99s", buf);
        if (buf[0]=='q')
            goto end;
        printf("You pressed %s\n", buf);
        printf("\n\n\n");     
        goto title;
    end:
        printf("Bye.\n");
        return 0;
    }
    As others mentioned, this would be much better expressed as a while loop.

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Have to echo that this is the wrong approach (the warning sign is goto which, while it has its uses, should be a big red cross in your head whenever you find yourself using it).

    What you probably want is a function, called title, that executes those bits of code. Then you call that function, just like you called clrscr(), in the places in the program that you need it. Then, if you want, you can loop around so you do it more than once.

    Read up on C functions and loops, and forget about using goto. I'm not a goto-hater (which some are since Dijkstra shouted his mouth off), but this is not a good use for goto when there are better ways of doing EXACTLY what you want using the proper functionality of C. Seriously. It makes me cringe. Stop it. And whoever you learned that from, give them a poke in the eye from me.
    shansajid likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,100
    Yes. One technique is just to write the code using your functions before you've actually written them. This is called top-down design.

    Code:
    while (true) {
        showTitle();
        showPrompt();
        char cmd = getCmd();
        if (cmd == 'f') {
            foo();
        }else if(cmd == 'b') {
            bar();
        }else if(cmd == 'q') {
            break;
        }else{
            showErrorMessage();
            pause();
        }
    }
    Then you can fill in the actual definitions afterwards for foo(), bar() etc. afterwards.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    back in BASIC days, Batch files, etc...GOTO and GOSUB was powerful, and one of the only options. Although GOTO is in the C, and useable, it is frowned upon because there are more stable options to use. Sych as functions, switches, loops, etc....

    you could use it, but i would recommend finding a more practical option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost ("short int" and "while" statements)
    By toadkiwi in forum C Programming
    Replies: 16
    Last Post: 02-29-2008, 07:15 PM
  2. Retrieving local name of "Users" group
    By Magos in forum C# Programming
    Replies: 1
    Last Post: 09-14-2007, 02:16 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  5. Replies: 18
    Last Post: 09-08-2001, 03:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21