Problem with goto

This is a discussion on Problem with goto within the C++ Programming forums, part of the General Programming Boards category; Code: teste: cout << "Nath, digite ate que numero deseja pesquisar por numeros primos.\n"; if(!(cin >> MAX)){ cout << "Erro! ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    Problem with goto

    Code:
    teste:
    cout << "Nath, digite ate que numero deseja pesquisar por numeros primos.\n";
    if(!(cin >> MAX)){
    cout << "Erro! Apenas numeros sao validos! (0123456789)\n";
    cin.clear();
    cin.ignore();
    goto teste;
    return(0);
    }
    Actually, if i dont input a number, the code is entering 3 times in the IF before ask for another input. (and it dont show the first "cout" message)
    Why?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Have you considered using a loop?
    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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Why use a loop? My code is supposed to work on this way..

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I can't remember, I don't use goto much, but I think

    teste:

    should be

    teste:;

    I could be absolutely wrong of course.

    The thing people say about goto is that you shouldn't really use it unless there isn't really a viable alternative. Here, as laserlight said, a loop would suffice, so why not just use one?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Questions and Suggestions:
    -- Use tabs (or at least spaces)
    -- I think that return(0) in the IF statement is useless
    -- MAX is an int?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Why use a loop?
    Structured programming.

    My code is supposed to work on this way..
    Write it as a loop first and post it. If you cannot even do that, you have no business talking about gotos.
    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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Yes the return is useless, i forgot to get rid of it.
    and yes, MAX is an int

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Tropicalia
    Why use a loop? My code is supposed to work on this way..
    Your code is more like assembly...

    Goto is quite "deprecated" feature in C++
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Incidentally, I do not really see anything wrong about the code (other than the use of an unnecessary goto). I decided to test, and the programs runs as I would expect. Rather strange that you had a problem.
    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

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    cout << "Nath, digite ate que numero deseja pesquisar por numeros primos.\n";
    
    while ( !(cin >> MAX) )
    {
    	cout << "Erro! Apenas numeros sao validos! (0123456789)\n";
    
    	cin.clear();
    	cin.ignore();
    
    	cout << "Nath, digite ate que numero deseja pesquisar por numeros primos.\n";
    }

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Because this use of goto is bad. There's a maybe a couple of situations where goto makes sense. Other than that it's best if you quickly learn how to use loops.

    Anyways, there is an error because if(!(cin >> MAX)) will return false if the user input is valid. That is why the got statement is never read. The if statement returns false.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    twomers. your code show the cout message in the while two times if I input one string.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Anyways, there is an error because if(!(cin >> MAX)) will return false if the user input is valid.
    That looks expected. It looks like that is intended to print an error message if the user input is invalid.
    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

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Yes... just noticed that. my bad.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Mario F.
    Anyways, there is an error because if(!(cin >> MAX)) will return false if the user input is valid.
    I thought of saying something about that IF statement, but I was afraid I'm wrong.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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