CIN inputing the wrong type

This is a discussion on CIN inputing the wrong type within the C++ Programming forums, part of the General Programming Boards category; How can you protect an integer defined input variable from a accidental keyboard charter input when using cin to read ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    11

    CIN inputing the wrong type

    How can you protect an integer defined input variable from a accidental keyboard charter input when using cin to read the keyboard? An accidental charter input seems to disable the cin or scanf function. The cin or scanf places 255 into the assigned int variable and then cin or scanf stop excepting input.

    int number;

    cin >> number;

    If you type any char [ a-z A-Z ] on the keyboard cin will put 255 in the int variable number and then the cin stops excepting input until you stop and restart the program.

    Does anyone know how to protect your program form this type of problem?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    cin won't put 255 in the variable. That's presumably what was already there.

    You'll have to do the usual "put the read statement in a while loop and if they do something stupid call cin.ignore() and cin.clear() and do it again" routine.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    11
    Thank you for the solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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