Thread: How do I stop cin from crashing my prog

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    How do I stop cin from crashing my prog

    say i have:

    int x;
    cin>>x;

    and i enter a char, the program crashes
    or if i enter 9999999999, it also crashes
    how do i stop that from happening?

    also, i want the data to be input without pressing enter
    i.e. for single digit input, i want to input the data as the key is pressed

    e.g. "Press any key to continue" *presses d*

    How do i achieve that?
    thanks
    I AM WINNER!!!1!111oneoneomne

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heres a hint: when you have:
    Code:
    int x;
    cin>>x;
    and you input a character cin gets set to a failed state. You can test for this state by calling the fail() member function. If its true its in a failed state. You can then clear() cin and remove the offending character and try the input again.

  3. #3
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    ok thanks
    where do i get a list of cin's member functions?
    I AM WINNER!!!1!111oneoneomne

  4. #4

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    thanks man
    ur da bomb
    I AM WINNER!!!1!111oneoneomne

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The downside of using the stream states analysis route is that when user inputs 1o1 instead of 101 the fail bit will not be set. Instead the value of 1 will be assigned to the variable. This has two potential problems. First, the user won't know that 1 was assigned to the variable instead of 101 (until some time later, maybe), and second, I believe the o1 will remain in the input buffer to potentially screw up your next input sequence.

    An alternative is to only accept input only as a string and parse the string for appropriateness. True, this isn't foolproof either. It also requires a little more coding on your part, particularly if you want to screen for float/doubles instead of just ints.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    another way to go at it is to use getch() and parse it as it comes in. again, this is going to mean more coding for you.

    usually I just take in a string, and if it fails simple validation, throw an exception, flush the string, and make the user input it again...

    and in reference to the topic of this thead:
    How do I stop cin from crashing my prog
    should be
    How do I stop dumb users from crashing my prog
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Except that getch() isn't standard

    User input validation can become a major pain in the arse to code for and I really didn't want to scare away the OP, yet.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Thantos
    Except that getch() isn't standard

    User input validation can become a major pain in the arse to code for and I really didn't want to scare away the OP, yet.
    yeah, I kinda cringed when I hit submit (or one of it's variations)... but elad has a good point with the 1o1 post...

    for example, say you have this:
    Code:
    int x,y;
    cin>>x>>y;
    but the input is:
    1o1
    552
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    But what if you had this:
    Code:
    char x;
    int y, z;
    cin>>y>>x>>z;
    Then an input of 1o1 would be just fine. Oh the funs of user validation

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Thantos
    But what if you had this:
    Code:
    char x;
    int y, z;
    cin>>y>>x>>z;
    Then an input of 1o1 would be just fine. Oh the funs of user validation
    then you could also always have this:
    Code:
    int ix,iy;
    float fx,fy;
    char c;
    
    cin>>ix>>c>>fx>>c>>iy;
    cin.ignore(1);
    cin>>fy;
    then an input of 1o1.5x3F3.1415 would be fine too...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does my prog just stop?
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 03-01-2006, 05:12 PM
  2. BST inser method seems to be crashing my prog
    By ray_broome in forum C++ Programming
    Replies: 12
    Last Post: 04-22-2005, 12:02 PM
  3. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  4. Crashing on prog quit
    By Hunter2 in forum Game Programming
    Replies: 11
    Last Post: 09-07-2002, 12:13 PM
  5. sprintf crashing my prog?
    By Eber Kain in forum C++ Programming
    Replies: 11
    Last Post: 11-21-2001, 04:25 AM