GOTO statement in c++

This is a discussion on GOTO statement in c++ within the C++ Programming forums, part of the General Programming Boards category; hello people first of all, im going to apologize for my bad english if there is, since im brazilian. My ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Exclamation GOTO statement in c++

    hello people

    first of all, im going to apologize for my bad english if there is, since im brazilian.

    My question is:

    I have read in various places the you should avoid using the GOTO statement at all costs when programming in C++. OK, but, in almost every of my programs I use about 10 GOTO statements because I think it is very useful, other people say otherwise. So im trying to end the habit of using GOTO, but, what can i use to replace this? I saw about using loops, such as do...while, while, for, if, functions and others. But I can't see how can I adapt the goto staments with those codes.
    For example, this program:


    Code:
    int main()
    {
    int chose;
    cout << "Hello, insert 1 to continue on the program or 2 to exit it";
    cin >> chose;
    
    // I would normally use a if or switch stament here, so here it is
    
    if (chose == 1)
    {
    cout << "The program will continue";
    }
    
    if (chose == 2)
    {
    return 0;
    }
    
    if (chose != 1, 2)
    {
    goto invalid_value //Here is the villain
    }
    
    invalid_value:
    
    blablablabla


    Get it? This of course is a very simple way to describe my problem, but for example, if in various parts of the program I have to insert 1 or 2 to do yada yada..., within or without a switch statement, how could i do the invalid_value "function" to be executed if i didn't put nor 1 or 2? What could I use in the place of GOTO?

    Thanks alot.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why not just do it?
    Code:
    if (chose != 1 || chose != 2) {
        /* do your invalid value stuff here */
    }
    If I'm reading your second question correctly (i.e. you have several places where you want to do the exact same thing), then yes you want to use a function. Just put all the statements you want to happen in an invalid_value case in a function (just like you put all your statements you want the program to do in your main function) and call it. If you're not familiar with functions, you can look in your textbook there, or read the tutorials here or elsewhere.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Thanks alot tabstop, u really helped me =D

    thx man

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    if (chose != 1 || chose != 2)
    That's a tautology. Since chose cannot be 1 and 2 at the same time, one of these must be true. I think you mean chose != 1 && chose != 2.

    A switch would be even better.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Quote Originally Posted by CornedBee View Post
    That's a tautology. Since chose cannot be 1 and 2 at the same time, one of these must be true. I think you mean chose != 1 && chose != 2.

    A switch would be even better.
    I don't know what a tautology is but I think you didn't fully understand my question. as tabstop wrote

    Code:
    if (chose != 1 || chose != 2)
    Produces the same effect as
    Code:
    if (chose != 1, 2)
    So, what I mean, is, if the person who is using the program inserts a different value from the one given, for example, 3, the program jumps, using the GOTO statement, to the invalid_value label. So i don't mean
    Code:
    if (chose != 1 && chose != 2)
    Tanks anyway.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by kibestar View Post
    So i don't mean
    Code:
    if (chose != 1 && chose != 2)
    But what is the purpose of
    Code:
    if (true)
    which is the same as
    Code:
    if (chose != 1 || chose != 2)
    if you really want to use or you can use
    Code:
    if (!(chose == 1 || chose == 2))
    But then. This again is the same as
    Code:
    if (chose != 1 && chose != 2)
    Kurt

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    Quote Originally Posted by kibestar
    I don't know what a tautology is
    A tautology is an expression (or formula) that always evaluates to true regardless of the possible values of the variables involved.

    Quote Originally Posted by kibestar
    Produces the same effect as
    No, tabstop did not say that. However, I think that you are right: the effect is the same, because (chose != 1, 2) evaluates to 2, and 2 evaluates to true.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What you are asking (flow of control) is covered in any rudimentary C++/programming book. Why does no one use books anymore when learning a language?

    This runs parallel to the other disturbing trend - people with very nascent programming skills trying to write 3D games, ostensibly because they're so much fun to play.
    Last edited by medievalelks; 03-22-2009 at 08:14 AM.

  9. #9
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    While C/C++ provide the 'goto' statement, that statement is effectively a 'copout' move -- e.g., it's considered bad programming because there is always a way to break out of a loop or execute conditional code without needing to jump around in your code. Only very low-level code should ever need to use it (I'm thinking ASM here) so I can't even think of a case in typical application development where using it would be necessary when there are other more robust and flexible approaches.

    -----

    In response to medievalelks, the problem is that many of those people you mention trying to write 3D games are typically young (e.g., 13 - 24) who think that writing a 3D engine is a trivial process. Part of it also comes from needing to have the pride of saying "Look! I made that!" instead of using other freely available code such as id's fantastic engines or other 3D engines like Ogre, CrystalSpace, etc.

    Unfortunately being young and eager almost always leads to skipping steps to get to the end goal which always leads to failure. Just look at how many project on GameDev fizzle and die and are never heard from again?
    - Leeor

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. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  3. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 09:37 PM
  4. Goto statement
    By _JjC:: in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 09:43 AM
  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

Tags for this Thread


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