Goto

This is a discussion on Goto within the C++ Programming forums, part of the General Programming Boards category; I need to know how to use the goto command, I have a list of 'ifs' something like this: int ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Cool Goto

    I need to know how to use the goto command, I have a list of 'ifs'
    something like this:


    int main()
    {
    cout << blah blah blah...if blah blah blah press 1, if blah blah
    blah press 2.

    if (x==1)
    cout << blah blah blah...if blah blah press 3, if blah blah press 4

    if (x==2)
    cout << blah blah blah...

    if (x==3)
    cout << blah blah blah...

    if (x==4)
    cout << blah blah blah...

    how then if I needed to go back to if (x==2) from somewhere later on would I get back to it. I have been told to use the goto thing, I was wondering if I can get some more opinions on this.

    Thanks.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    mylabel:
    //Other stuff
    goto mylabel;

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    This is a old example of creating spaghetti instead of well structured software. You should not use goto. It seems you are creating a large if-construction. I would propose to use the switch-statement and functions.

    Whenever it happens that (x == 2), you just call the function that belongs to it.

    The switch-statement:

    Code:
    switch (x)
    {
        case 1:
            /* do something */
           break;
        case 1:
            /* do something */
           break;
        etc.
        ...
        default:
            break;
    }

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, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09: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

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