Thread: The goto doesnt work!

  1. #1
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100

    The goto doesnt work!

    After all that other posts that I did, the goto that I tried to do the other day didnt work today, here is the complete code:

    ------------------------------------------------------------------------------------
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    void main(void)
    {
    int X;
    A:
    X=X+1;
    if (X==10000000)
    goto O; //It says errors are here
    char game4;
    int game1=rand()%3;
    if (game1==1)
    {
    int game2=rand()%10;
    cout<<game2;
    goto A;
    }
    int game3=rand()%8;
    if (game3 == 1)
    game4 = '!';

    if (game3 == 2)
    game4 = '@';

    if (game3 == 3)
    game4 = '#';

    if (game3 == 4)
    game4 = '$';

    if (game3 == 5)
    game4 = '%';

    if (game3 == 6)
    game4 = '^';

    if (game3 == 7)
    game4 = '*';
    cout<<game4;
    goto A;
    O: //and here
    return (0);
    }
    ------------------------------------------------------------------------------------

    Some one help me by telling me how to make it jump down the program using goto's please.
    Last edited by Esparno; 03-06-2002 at 06:31 AM.
    Signature is optional, I didnt opt for one.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    perhaps try using a complete word for a label and not just the letter O.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Ok, ive finally figured it out, I cant have the declaration statements of the variables in between the goto statements, that is, I cant "jump over" declaration statements. Ive finally got it!
    Signature is optional, I didnt opt for one.

  4. #4
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    if (X=10000000)
    goto O; //It says errors are here


    you are assigning not comparing, put another = in between
    x==100000

    except for this, your code seems alright.
    -

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    363
    Go to is evil and should be avoided at all costs!

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    yah yah

    si seņor... it creates more problems, than it solves.. i don't know where your trying to go by using "GOTO" statements... why not try a different programming language.. like some old' assembly language somewhere... learning "goto" in c++ is going down the wrong road...

  7. #7
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Im sorry, the power of the "Dark side" of goto has me in its grasp already, I cant get out of it, its too late for me.....
    Signature is optional, I didnt opt for one.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> I cant get out of it, its too late for me.....

    Well there goes any chance of a professional career...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    What do you mean there goes any chance of a profesional career?
    Signature is optional, I didnt opt for one.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    he is saying that using the goto command in C++ is a very bad habit, and will only cause problems later.

    i would have to agree. in C++ there is absolutely no reason to use goto, since you can merely structure your code to go back and forth exactly like you want it to.

    and once you learn how to do that, you will never use goto again.

  11. #11
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    How do you structure the code to switch back and forth without using goto?
    Signature is optional, I didnt opt for one.

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> is a very bad habit

    It is a bit more than that I'm afraid. I have never yet worked for a company which did not specifically ban the use of the goto statement in it's house coding standards.

    >>> without using goto?

    There is no construction that you can do with goto that you cannot do with another language construct. Give an example of what you mean, and someone will show you a better way.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #13
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Using the other control statements in the language.
    Mainly, switch statements, while and do while loops, in addition to if and else clauses.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  14. #14
    His posts are far and few Esparno's Avatar
    Join Date
    Mar 2002
    Posts
    100
    Ok, so how would i change this bit of example code into proper code?



    A:
    if (thing1==100)
    goto B;
    game1=3000;
    goto A;

    B:
    thing2=10;
    goto A;
    Signature is optional, I didnt opt for one.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Man, I havent used goto since 1988 in BASIC on my old Amstrad CPC464.
    The memories.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GOTO statement in c++
    By kibestar in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2009, 07:10 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  5. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM