Thread: Numeric checking

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    3

    Question Numeric checking

    How can I validate a each character of an array to check if it is numeric? ie

    if (num[1] != 0)
    ......

    but I want to check against all digits, 0-9 using one line of code? I know there is a way like, [0-9], but cannot find it?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    3
    I don't want to use isdigit!

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    Originally posted by spaceboo
    I don't want to use isdigit!
    And why that?
    so build your own one, that I think this should be very close to the isdigit one.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    I've done something like this before using ascii values. As '0' has as ascii value of 48, and '9' has an ascii value of 57, then...

    Code:
    n = buf[x] - 48;
    if(    ( n < 10 )
      && ( n >= 0 ))
    then buf[x] is a digit.

    So get your string in a buffer, set up a for loop, and for each character in the string, do the above check.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I'm sure anything you make will be very close to isdigit(). Although if you have a real need of 'one line comparison' I suppose you can create a regexp in C and compare it to that. Heh.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How can I validate a each character of an array to check if it is numeric? ie

    if (num[1] != 0)
    ......

    but I want to check against all digits, 0-9 using one line of code? I know there is a way like, [0-9], but cannot find it?
    If I were avoiding isdigit, I would do this.
    Code:
        if(num[1] >= '0' && num[1] <= '9')
            /* ... */
    Using '0' and '9' is more portable than using their ASCII equivalents 48 and 57 because it need not be ASCII.

    One "[0-9]" method is as follows.
    Code:
        char digit[2];
        if(sscanf(&num[1], "%1[0-9]", digit) == 1)
            /* ... */
    Although the %[0-9] is a common extension, it is non-standard. A standard version would be like this.
    Code:
        if(sscanf(&num[1], "%1[0123456789]", digit) == 1)
            /* ... */
    It meets the "one line" criteria (if you declare digit with num), but to me it seems quite excessive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Checking if a var is numeric?
    By Glauber in forum C++ Programming
    Replies: 21
    Last Post: 05-18-2008, 12:51 PM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. checking for valid numeric field..
    By ronkane in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2002, 09:04 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM