How To Check For A Char Val......

This is a discussion on How To Check For A Char Val...... within the C++ Programming forums, part of the General Programming Boards category; Ok im writting a program that asks user for ONLY numaric values, and i want to display an error message ...

  1. #1
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52

    How To Check For A Char Val......

    Ok im writting a program that asks user for ONLY numaric values, and i want to display an error message if the user hits any alpha keys.... i know theres probably a function for this.. but i dont know it...

    lol VB makes it nice and easy "If IsNumeric ....."

    anyways anyhelp would be great

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    isdigit
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    isdigit ? like a function already in the std?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    #include <cctype>
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    ok.. so if i use that header will that allow me to acess the "isdigit" thing....lol "thing" such a technical term hu

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    just make sure you cast the character to an unsigned char prior to sending it to isdigit

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I know of no way to prevent a user from entering an alpha key when you only want them to enter a digit key. What you can do is validate the input before using it. There are several ways to do this. Since all keys can be entered as char types you can accept input as a char and then use isdigit to see if it is a digit. If the char is a digit and you wish to use it as a char, fine. If you wish to use it as a numeric value, for example as an int or a double, etc., then you need to convert it to the appropriate type after validation. Alternatively, you can "assume" the user will be accurate in their input and accept the input directly into a variable of numeric type and check the state value of the stream after the input to verify that the input was correct. Both approaches have their limitations, so use whichever one you feel is best suited to your purpose.
    Code:
    //accept input as a char
    char input;
    cout << "enter a digit" << endl;
    cin >> input;
    if(isdigit(input))
      cout << "thank you" << endl;
    else 
      cout << "input was not a digit." << endl;
     
    //one way to convert char to int
    //first convert char to string
    //one way to convert char to string
    string str = "";
    str += input;
     
    //then convert string to int
    int x;
    x = atoi(str.c_str());
     
    //example of using streams to validate input
    int x;
    cout << "enter an integer value" << endl;
    cin >> x;
     
    //check fail state of stream
    if(cin.fail())
    {
       cout << "invalid input" << endl;
       cin.clear();  //clear stream for reuse
       cin.ignore();  //ignore the invalid input
    }
    Discussions on data validation can get much more detailed/involved, so depending on your needs you may want to obtain additional information.
    You're only born perfect.

  8. #8
    Fallen AndyBomstad's Avatar
    Join Date
    Jan 2005
    Posts
    52
    ok guys all that helped and ive got a slightly working code
    Code:
    #include<iostream>
    
    #define cls system("cls");
    #define pause system("pause");
    
    using namespace std;
    
    int main()
    {
        
        char input;
        cout << "enter a digit" << endl;
        cin >> input;
        if(isdigit(input)){
                           cls;
                           cout << "You Entered The Number: " << input<<endl;
                           pause;
                           cls;
                           main();}
             else{ 
                   cls;
                   cout << "Error! Must Enter A Number" << endl;
                   main();}
             
        pause;
    }

    but im limited with this program, if i try to enter a double digit number its only picking up the first number because of type Char.....how do i get it to catch more than 1 number?

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Read in an entire line of input using getline(). Check to make sure that the input consists of just a number. Then convert it to a number.

    FAQ > How do I... (Level 1) > Convert a string to a int (C++)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    FAQ > How do I... (Level 2) > Why does my program enter an infinite loop if the user inputs invalid data? (C++)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM

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