Thread: Library Function to verify Int

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Library Function to verify Int

    I have a function that will only work with integers. Is there a library function that will check user input to make sure that the data entered is integers.

    [EDIT]
    Currently I am using a bool function that uses 'isalpha()' returning true and false and I wondering if there is something simpler
    [\EDIT]


    Thanks.
    Last edited by Mr_roboto; 09-29-2005 at 01:29 AM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    isdigit()?

    Code:
    if(isdigit(ch) && ch > INT_MIN && ch < INT_MAX)
    Last edited by sand_man; 09-29-2005 at 03:25 AM.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    If you want a function to validate user input while typing and ignore non-digit keyboard entries, then no, there is no standard function to do that. You have to write your own. isalpha() will not tell you whether a character is a digit or not -- use isdigit() for that. isalpha() indicates that a character is alphabetic ('a'-'z' or 'A' - 'Z'), and those are 52 out of possible 255. So when isalpha() fails doesn't mean that the character is a numeric digit.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Another easy way to check for an int that works better than isdigit is to create a stringstream and attempt to extract the int from the stringstream. If it fails or has leftover characters then it is not an int.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Thank you

    Awsome tips guys.

    Thank you.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Daved
    Another easy way to check for an int that works better than isdigit is to create a stringstream and attempt to extract the int from the stringstream. If it fails or has leftover characters then it is not an int.
    I haven't used stringstream very often -- how can you tell that the string contained non-decimal characters? For example " 12ab34". atoi() just returns 12, and stringstream does the same. So, without checking the number of digits in the integer against the length of the original string, how do you know stringstream could not convert all the characters? I know the fail() will return true if none of the string can be converted -- such as "a123".

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    With the stringstream you just check if there is anything left in the stream. I don't know the best way to do this. After extracting into the int, you could extract into a string and see if the string is empty, or maybe you could use get() or peek() to see if there is anything left. Obviously, for "12", the stream will be empty, for "12ab34" it will have "ab34".

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I've tried this.
    Code:
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    bool isint( const string& s ) {
       stringstream str;
       string srest;
       int i;
       str << s;
       str >> i;
       str >> srest;   
       if ( srest.length() > 0 )
          return false;
       str.str("");
       str.clear();
       str << i;
       return str.str() == s;
    }
    
    int main() {
       string str("12ad34");
       
       if ( isint( str ) )
          cout << str << " is an int" << endl;
       else
          cout << str << " is not an int" << endl;
          
       str = "123456899999999";
       
       if ( isint( str ) )
          cout << str << " is an int" << endl;
       else
          cout << str << " is not an int" << endl;
       
       str = "123459";
       if ( isint( str ) )
          cout << str << " is an int" << endl;
       else
          cout << str << " is not an int" << endl;
    }
    Without that second check in isint() overflow would not be detected. ( I'm using g++ 3.3.5 )
    Kurt

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    This is still a lot easier on the eyes. It doesn't have all that messy stringstream stuff.
    Code:
    bool isint( const string& s ) {
      for(int i = 0; i < s.length(); i++)
      {
         if( !isdigit(s[i]))
             return false;
      }
      return true;
    }

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks a lot better BUT:
    a) Doesn't detect overflow.
    b) returns false on negative ints ( ok. my version returns false if there ise a + sign )
    Kurt

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    c) fails on hexadecimal
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    checking for sign can be easily fixed.
    Code:
    bool isint( const string& s ) {
      int i = 0;
      if( s[0] == '-' || s[0] == '+')
         i++;
      for(; i < s.length(); i++)
      {
         if( !isdigit(s[i]))
             return false;
      }
      return true;
    }
    The way I understand it, the function is supposed to determine if the string contains all digits or not. It doesn't give a hoot about integer overflow, which is a different problem altogether.
    Last edited by Ancient Dragon; 09-29-2005 at 12:34 PM.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Stoned_Coder
    c) fails on hexadecimal
    Of course it will -- hex is not an integer string of base 10!

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes it is but its in base 16. its a valid int. oxff is 255 for example and can be passed to a func requiring an int.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM