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

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    ANY backwards goto is a sign that you really ought to be using a loop construct of some kind.
    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.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    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.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  5. #5
    spaghetticode
    Guest
    Quote Originally Posted by shansajid View Post
    How can I use "goto" for group of multiple statements
    Don't.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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.

    - 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,393
    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
    307
    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, 08: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, 08: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, 07:59 AM
  5. Replies: 18
    Last Post: 09-08-2001, 03:15 AM