I know everyone hates "GOTO" but...

This is a discussion on I know everyone hates "GOTO" but... within the C++ Programming forums, part of the General Programming Boards category; I am only new and learning and am trying to use a GOTO functiong in my code but dont know ...

  1. #1
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14

    I know everyone hates "GOTO" but...

    I am only new and learning and am trying to use a GOTO functiong in my code but dont know how to create the label that GOTO is meant to...well go to.

    any help is as allways appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    with "label:", as in
    Code:
    start:
    //do something
    middle:
    //something else
    end:
    return 0;

  3. #3
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    when i put that form of code in the following form:

    Code:
    int main (void) 
    {
    top:
    //....
    }
    int second (void)
    {
    //... 
    if //function
    GOTO top
    this does not work the error says label top is used but not defined.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    Depends, what do you want to do?
    I'm just a beginner in C++ but i don't think jumping out of functions with a goto is a good idea

  5. #5
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    do you know how to jump from a function to a previous function depending on a variable such as:

    Code:
    if answer = yes
    GOTO //jump back to start of main function
    else EXIT
    basing it that this code is in a second function and i want to jump back to the main function.
    Last edited by anderson; 02-13-2006 at 05:19 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    // The keyword is
    
    goto
    
    // not
    
    GOTO
    Most IDEs will bolden keywords.
    Last edited by SlyMaelstrom; 02-13-2006 at 05:33 AM.
    Sent from my iPadŽ

  7. #7
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    yeah in my actual source code i dont have it capitalized....

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well then pasting something that isn't your source code and asking why your source code is getting errors doesn't make much sense, does it.

    ...and labels, like variables have scope. Which is why this:

    Code:
    void func();
    
    int main () 
    {
       top:
         func();
    }
    
    void func() 
    {
       goto top;
    }
    doesn't work.
    Last edited by SlyMaelstrom; 02-13-2006 at 05:39 AM.
    Sent from my iPadŽ

  9. #9
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    ok i get wat your saying and yes it was stupid of me but what im saying is how do i jump out of a second function and back into the beggining of the first?

    or is it possible to make the application start over completly based on results from
    Code:
    	 if answer = yes
    		restart program
    		else   exit program
    Last edited by anderson; 02-13-2006 at 05:46 AM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You would base your goto off the return of the function.

    Code:
    #include <iostream>
    
    int func(int);
    
    int main () 
    {
      int x = 0;
      
      top:
        x = func(x);
        if (x == 1)
          goto top;
      
      return 0;
    }
    
    int func(int var1) 
    {
       if (var1 == 0) {
          std::cout << "Going to top\n";
          return 1;
       }
       std::cout << "Not going to top";
       return 0;
    }
    Last edited by SlyMaelstrom; 02-13-2006 at 05:53 AM.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Even if labels didn't have scope and you could jump into another function it would cause problems with the stack. The stack is set up with local function variables, arguments and the return address of the function you are in, if you jumped into another function these would all be incorrect.

    You don't need goto! Your example could be coded like this
    Code:
    int main (void) 
    {
    bool bExit=false;
    while (!bExit)
       bExit=Second();
    }
    bool Second (void)
    {
    ...
    //if answer=="yes";
    return false;//don't exit
    else
    return true;//do exit
    Last edited by Quantum1024; 02-13-2006 at 05:54 AM.

  12. #12
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    k thnx

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    goto's r good,i just love goto's,goto's give your code just the chic look u always wanted in it,and if u have mastered goto,come to OOP,thats the latest trend,make everything an object,do some fanciful interfaces,now u r realizing art.







    __________________________________________________ __

    "its not programming if its not oop"

  14. #14
    Noob Coder anderson's Avatar
    Join Date
    Feb 2006
    Posts
    14
    wouldnt it make the code slightly more complicated if everything was made an object and every movement was defined by goto???
    Code:
    source knowldege;
    	 source from==goverment
    	 if return=1
    		 affirm lie
    	 else affirm truth

  15. #15
    Registered User Voodoo Doll's Avatar
    Join Date
    Feb 2006
    Posts
    4
    do you know how to jump from a function to a previous function depending on a variable
    You can use a stronger goto in the form of setjmp and longjmp, but that's ugly and awkward. A better option is to throw an exception. Both of those will safely unwind the function call stack and take you back to where you want to be.

    But those are for exceptional situations, ie. errors and unexpected stuff. For normal operation, the return value of a function that drives a loop is more intuitive.
    wouldnt it make the code slightly more complicated if everything was made an object
    Yes.
    and every movement was defined by goto???
    Not necessarily. That's the way assembly works and I find it easier to read and write than C++. I guess it depends on the idioms that a language best supports that determine how complicated unconditional jumps are.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I think my compiler hates me
    By Matty_Alan in forum C Programming
    Replies: 4
    Last Post: 07-15-2007, 01:22 AM
  2. Pet Hates
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-21-2006, 05:08 AM
  3. The taskbar hates me messing around!
    By Queatrix in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2005, 08:58 PM
  4. Who on this board hates me?!
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 01-28-2003, 03:41 PM
  5. Cat hates getting a bath
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 10-23-2001, 09:15 PM

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