Thread: keyboard error handling

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    keyboard error handling

    I am writing a simple program for school that lets a user input a number and it tells them if they are too high or too low. But I going to be tested for all situations, including the user entering an invalid key. I was wondering if there was any way that you could handle the user pressing a letter on the keyboard. I'm new to this so any help would be greatly appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    There are several different ways. One is to use the fail(), clear(), ignore() members of the istream class. Another is to accept input into a string, validate the string, then convert the string to desired type. There may be others as well.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    I would opt for the latter option. It could be done somewhat like this:
    Code:
    bool ok = true;
    string input;
    getline(cin, input);
    
    for(int i = 0; i < input.size(); ++i)
        if((input[i] - '0') > 9)
            ok = false;
    
    if(!ok)
        cout << "Failed";
    
    // other code...

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I would like to recommend the isdigit( ) function of the <cctype> library. It is a boolean function that will return true if the argument is a number.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Well I thought it wasn't standard..

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    isdigit() returns a non-zero value when the input parameter is a numberic digit, not true. But the point is still the same. And it is standard.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    to tilex... would i put that as just its own separate block of code in int main() ?

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    You should be able to figure that on your own, I've already said more than I should have. Study the code and then when you know what it does, you'll know where it goes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. : Difference between message queue and keyboard buffer...
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-21-2002, 03:47 PM