Thread: Problem with goto

  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
    28,413
    Have you considered using a loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,273
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,273
    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
    Ireland
    Posts
    8,446
    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.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    Yes... just noticed that. my bad.
    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

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, 06: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, 05:46 PM