Results 1 to 5 of 5

Thread: characters entered when numbers wanted

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    characters entered when numbers wanted

    How do you check that when i use cin to get a number, the user doesn't enter a character? when this happens, the screen goes crazy, and i'd like to protect against this.

    thanks,
    jmiller

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The screen goes crazy?

    Code:
    int i;
    cin >> i;
    if(cin.good())
      // i contains a valid value
    else
      // i doesn't contain a valid value
    If it's not valid you'll have to clear cin's input buffer. I posted a line of code that does this a few days ago.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    It gets stuck on the character, and then application goes nuts because it does not like the character.

  4. #4
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    Whoa never heard of cin.good, well i guss u learn something new every day. jMiller check this page out, u'll find some pretty interesting functions.
    http://vbforums.com/showthread.php?s=&threadid=228450
    Death is always smiling down on us, the only thing we can do is smile back

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And another thread which is more interesting for your problem.
    http://www.vbforums.com/showthread.p...ghlight=ignore
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width