Problem with goto

This is a discussion on Problem with goto within the C++ Programming forums, part of the General Programming Boards category; >> twomers. your code show the cout message in the while two times if I input one string. Get rid ...

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> twomers. your code show the cout message in the while two times if I input one string.

    Get rid of one then. I don't understand the language, so I didn't know what it said. So I didn't know if it was valid in the loop.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    In twomers code, if the input has 1 char, it gives one error message, if the input has 2 chars it gives two error messages, and there you go...

    Code:
    cout << "Input a number please.\n";
    
    while ( !(cin >> MAX) )
    {
    	cout << "Error! Input numbers please! (0123456789)\n";
    
    	cin.clear();
    	cin.ignore();
    
    	cout << "Input a number please.\n";
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    twomers. your code show the cout message in the while two times if I input one string.
    twomers' code looks fine. I suggest that you post the smallest and simplest compilable program that demonstrates what you want to do.
    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

  4. #19
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by laserlight
    twomers' code looks fine. I suggest that you post the smallest and simplest compilable program that demonstrates what you want to do.
    Twomers code still uses that bad IF statement.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Wow, fastest thread ever! About one post per minute!!


    Mario (or laserlight, or anyone really), where would be a viable situation to use goto. I've read that major loop nesting is a good one. I can't think of any others really.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    In twomers code, if the input has 1 char, it gives one error message, if the input has 2 chars it gives two error messages, and there you go...
    Oh, I see what you mean. The solution is to use cin.ignore(1000, '\n') instead of just cin.ignore(). Instead of 1000, you could use std::numeric_limits<std::streamsize>::max()
    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. #22
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I have seen some good uses of goto, but I don't remember where I saw them.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #23
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    #include <limits> 
    
    using namespace std;
    
    int main( void )
    {
    	int MAX;
    
    	while ( (cout << "Input a number please.\n") && !(cin >> MAX) )
    	{
    		cout << "Error! Input numbers please! (0123456789)\n";
    
    		cin.clear();
    		cin.ignore(std::numeric_limits < int >::max(), '\n');
    	}
    }
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Well, now it works =]
    Thanks for all you guys :P

  10. #25
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by twomers
    Wow, fastest thread ever! About one post per minute!!
    I looked at this thread for 10 seconds, pressed refresh and 2 new posts!
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #26
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    (48-23)/25 wow! 1 post per minute

  12. #27
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    > Mario (or laserlight, or anyone really), where would be a viable situation to use goto. I've read that major loop nesting is a good one. I can't think of any others really.

    I can't think two situations of the top of my head.

    One is when you want to break completely out of nested loops. a break statement only breaks from the innermost loop. Of course an alternative was to keep a flag and test against it in each loop. But a goto can be important especially on situation where you want to extract every bit of performance out of your loops.

    Another is less common... but far more important. When generating code from a grammar through a parser. If you design a script language that generates code in the fly, you will find the use of goto commands quiet useful in your C++ script parser.
    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.

  13. #28
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Code:
    return(0)
    should be

    Code:
    return 0
    But this may depend on your environment, I am just stating what is classed as standard

  14. #29
    Registered User cdkiller's Avatar
    Join Date
    Feb 2006
    Posts
    20
    the only place i use goto is to write programs on my calculator and the PIC microprocessor (asm). You should listen to those who urged you to use a loop instead of goto. It is not recommended to use goto since its execution causes an unconditional jump ignoring any type of nesting limitations. goto really does not have a place in structured or object oriented programming.

  15. #30
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    @cdkiller:

    I use goto in graphic programming, for example:

    Code:
    gotoxy(5,1);cout << "PLOT THIS STRING HERE";
    But I do see your point, goto is good at plotting coordinates like above.
    But I have never used it in any sort of "other" C++ programming.

Page 2 of 3 FirstFirst 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