Thread: Need help with goto in c++

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    24

    Question Need help with goto in c++

    I`ve tried the tutorials, but i couldn`t find anything about goto. I don`t know how to set labels and i don`t know how to use goto. Examples would really help.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    why do you want to use goto? there really is not a very big use for it. A ussally better solution for goto is a function call
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    24

    Question

    Well, i don`t really know what you`re talking about when you talk about 'function calls' but i`m currently working on a game-like thing, for which i think i need goto. But how do I set labels? And do i need to use
    Code:
    goto(label);
    or
    Code:
    goto label;
    or something else?
    Last edited by darealnash; 07-05-2004 at 01:43 AM. Reason: i made a mistake

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    here's is how a function call would work
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        void CallFunction();
        cout<<"Calling Function";
        CallFunction();
        return 0;
    }
    void CallFunction()
    {
        cout<<"Function Called";
    }
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    Do function calls make the program go to a certain part of the code (similar to goto) or something else? Because i need to repeat a certain part of the code after a certain input.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    oh for that you would use a loop eg.
    Code:
    int input = 0;
    while(input==0)
    {
        cout<<"hey input still equals 0";
        cout<<"What do you want input to be?";
        cin>>input;
    }
    Woop?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    24

    Question

    I don`t think thats what i mean, but here`s my code:
    Code:
    #include <iostream.h>
    int main()
    {
      int input;
      cout<<"This game simulates a rich life."<<endl;
    1 cout<<"Input menu function"<<endl;
      cout<<"1: Spend money."<<endl;
      cout<<"2: Play a video game."<<endl;
      cout<<"3: Drive around in a car"<<endl;
      cout<<"4: Climb Mount Everest"<<endl;
      cout<<"5: Fly around in a helicopter"<<endl;
      cout<<"6: Kill yourself"<<endl;
      cin>>input;
    3
      if (input==1)
      {
        cout<<"You buy a helicopter, a building, and a private island."<<endl;
        goto 1;
      }
      else if (input==2)
      {
        cout<<"You turn on your Zcube and play for half an hour."<<endl;
        goto 1;
      }
      else if (input==3)
      {
        cout<<"You drive around in your $900,000,000.95 limo."<<endl;
        goto 1;
      }
      else if (input==4)
      {
        int action;
        cout<<"You climb Mount everest. Now what?"<<endl;
    2   cout<<"1: Shout:'I`m the king of the world!!!'"<<endl;
        cout<<"2: Go back down"<<endl;
        cin>>action;
        if (action==1)
        {
          cout<<"I`m the king of the world!!!"<<endl;
          goto 2;
        }
        else if (action==2)
        {
          goto 1;
        }
        else
        {
          cout<<"Please input menu functions only"<<endl;
          goto 2;
        }
      }
      else if (input==5)
      {
        cout<<"You fly around in your helicopter."<<endl;
        goto 1;
      }
      else if (input==6)
      {
        goto 3;
        return 0;
      }
    }
    Do you know what i`m doing wrong here?

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    you want to do this instead its much cleaner and easier to read
    Code:
    #include <iostream>
    using namespace std;
    void menu();//Function Protypes
    void mountain();
    int main()
    {
      menu();
      return 0;
    }
    void menu()
    {
      int input;
      cout<<"This game simulates a rich life."<<endl;
      cout<<"Input menu function"<<endl;
      cout<<"1: Spend money."<<endl;
      cout<<"2: Play a video game."<<endl;
      cout<<"3: Drive around in a car"<<endl;
      cout<<"4: Climb Mount Everest"<<endl;
      cout<<"5: Fly around in a helicopter"<<endl;
      cout<<"6: Kill yourself"<<endl;
      cin>>input;
      switch(input)
      {
        case 1:
          cout<<"You buy a helicopter, a building, and a private island."<<endl;
          menu();//Recursively calling menu
          break;//Keeps One case from going into the other
        case 2:
          cout<<"You turn on your Zcube and play for half an hour."<<endl;
          menu();
          break;
        case 3:
          cout<<"You drive around in your $900,000,000.95 limo."<<endl;
          menu();
          break;
        case 4:
          {//have to use braces when declaring a varible
            int action;
            cout<<"You climb Mount everest. Now what?"<<endl;
            cout<<"1: Shout:'I`m the king of the world!!!'"<<endl;
            cout<<"2: Go back down"<<endl;
            cin>>action;
            if(action==1)
            {
              mountain();
            }
            if(action==2)
            {
              menu();
            }
            break;    
          }
        case 5:
          cout<<"You fly around in your helicopter."<<endl;
          menu();
          break;
        case 6:
          cout<<"You Killed Yourself"<<endl;
          break;
      }
    }
    void mountain()
    {
      cout<<"I`m the king of the world!!!"<<endl;
      cout<<"Now what?"<<endl;
      cout<<"1: Shout agian"<<endl;
      cout<<"2: Go back down"<<endl;
      int mAction;
      cin>>mAction;
      if(action==1)
      {
        mountian();
      }
      if(action==2)
      {
        menu();
      }
    }
    Woop?

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    "untitled1.cpp": E2451 Undefined symbol 'action' in function mountain() at line 70
    "untitled1.cpp": E2268 Call to undefined function 'mountian' in function mountain() at line 72
    "untitled1.cpp": E2134 Compound statement missing } in function mountain() at line 77

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    sorry change the action to mAction
    Code:
    void mountain()
    {
      cout<<"I`m the king of the world!!!"<<endl;
      int mAction;
      cout<<"Now what?"<<endl;
      cout<<"1: Shout agian"<<endl;
      cout<<"2: Go back down"<<endl;
      cin>>mAction;
      if(mAction==1)
      {
        mountain();
      }
      if(mAction==2)
      {
        menu();
      }
    }
    Last edited by prog-bman; 07-05-2004 at 03:02 AM.
    Woop?

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    "untitled1.cpp": E2268 Call to undefined function 'mountian' in function mountain() at line 72
    "untitled1.cpp": E2134 Compound statement missing } in function mountain() at line 77

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    here's the full thing.
    Code:
    #include <iostream>
    using namespace std;
    void menu();//Function Protypes
    void mountain();
    int main()
    {
      menu();
      return 0;
    }
    void menu()
    {
      int input;
      cout<<"This game simulates a rich life."<<endl;
      cout<<"Input menu function"<<endl;
      cout<<"1: Spend money."<<endl;
      cout<<"2: Play a video game."<<endl;
      cout<<"3: Drive around in a car"<<endl;
      cout<<"4: Climb Mount Everest"<<endl;
      cout<<"5: Fly around in a helicopter"<<endl;
      cout<<"6: Kill yourself"<<endl;
      cin>>input;
      switch(input)
      {
        case 1:
          cout<<"You buy a helicopter, a building, and a private island."<<endl;
          menu();//Recursively calling menu
          break;//Keeps One case from going into the other
        case 2:
          cout<<"You turn on your Zcube and play for half an hour."<<endl;
          menu();
          break;
        case 3:
          cout<<"You drive around in your $900,000,000.95 limo."<<endl;
          menu();
          break;
        case 4:
          {//have to use braces when declaring a varible
            int action;
            cout<<"You climb Mount everest. Now what?"<<endl;
            cout<<"1: Shout:'I`m the king of the world!!!'"<<endl;
            cout<<"2: Go back down"<<endl;
            cin>>action;
            if(action==1)
            {
              mountain();
            }
            if(action==2)
            {
              menu();
            }
            break;    
          }
        case 5:
          cout<<"You fly around in your helicopter."<<endl;
          menu();
          break;
        case 6:
          cout<<"You Killed Yourself"<<endl;
          break;
      }
    }
    void mountain()
    {
      cout<<"I`m the king of the world!!!"<<endl;
      int mAction;
      cout<<"Now what?"<<endl;
      cout<<"1: Shout agian"<<endl;
      cout<<"2: Go back down"<<endl;
      cin>>mAction;
      if(mAction==1)
      {
        mountain();
      }
      if(mAction==2)
      {
        menu();
      }
    }
    Woop?

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    24
    Thanks!!!

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There is no reason to call menu recursivly.
    Code:
    void menu()
    {
      int input;
      bool keepalive = true;
      do {
        cout<<"This game simulates a rich life."<<endl;
        cout<<"Input menu function"<<endl;
        cout<<"1: Spend money."<<endl;
        cout<<"2: Play a video game."<<endl;
        cout<<"3: Drive around in a car"<<endl;
        cout<<"4: Climb Mount Everest"<<endl;
        cout<<"5: Fly around in a helicopter"<<endl;
        cout<<"6: Kill yourself"<<endl;
        cin>>input;
        switch(input)
        {
          case 1:
            cout<<"You buy a helicopter, a building, and a private island."<<endl;
            break;//Keeps One case from going into the other
          case 2:
            cout<<"You turn on your Zcube and play for half an hour."<<endl;
            break;
          case 3:
            cout<<"You drive around in your $900,000,000.95 limo."<<endl;
            break;
          case 4:
            {//have to use braces when declaring a varible
              int action;
              cout<<"You climb Mount everest. Now what?"<<endl;
              cout<<"1: Shout:'I`m the king of the world!!!'"<<endl;
              cout<<"2: Go back down"<<endl;
              cin>>action;
              if(action==1)
                mountain();
              break;    
            }
          case 5:
            cout<<"You fly around in your helicopter."<<endl;
            break;
          case 6:
            cout<<"You Killed Yourself"<<endl;
            keepalive = false;
            break;
        }
      }while(keepalive};
    }
    As for the syntax for goto it is simply this:
    Code:
    goto_label:
    goto goto_label;

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I find it often helpful to make my entire program just one huge main function, with all my variables global, and then just use goto jumps to get where I need. The benifit of this and the globals, is that on the off chance main gets too big, I can always just make one more big function, and all the variables are accessable to it. This lends greatly to the overall readability of the program.

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

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