Thread: I read the FAQ on isdigit() but...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    I read the FAQ on isdigit() but...

    ...I still do not understand how to check a user's input to ensure that it is a numeric character. I want to use the isdigit() function and I read the FAQ regarding the syntax of this function, but either the FAQ is too ambiguous or I'm not grasping the concept. (Probably the latter). I know what the isdigit() function is supposed to do -- and it's exactly what I need for my program -- but writing the blasted thing is my problem. I need more than just:

    #include <ctype.h>

    int isdigit(int c);

    to help me write the function.

    Furthermore, I want this thing to work inside a loop that already checks the input to make sure it falls within the range of acceptable numbers. Now I need the isdigit() function to first make sure the input is a number and not a letter, then it can check to see if it is within the range of acceptable numbers.

    Any help would be appreciated.

    CaptainJack

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    you should check for a digit by it's ascii value.

    if (c>'48'||c<'57')
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You should NOT check for a digit by its ascii value (nothing says the computer has to use ascii) and even then the code shown won't work...

    I'm not totally sure I understand the original question. Maybe posting some of the code you are working on would help explain.

    In the meantime take a look at this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    void checkChar(char ch)
    {
      if (isdigit(ch)) {
        printf("%c is a number\n", ch);
      } 
      else {
        printf("%c is not a number\n", ch);
      }
    }
    
    int main ( void )
    {
      char c1 = 'a';
      char c2 = '3';
    
      checkChar(c1);
      checkChar(c2);
    
      return 0;
    }
    HTH
    DavT
    -----------------------------------------------

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    so what is wrong with checking it's ascii value?
    would it run slower?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    ASCII is not the mandatory representation for characters in C
    (although I will admit it is widely used). What is mandated in
    the C specification is that numbers are coded sequentially, so
    you could do this:
    Code:
    if ((ch >= '0') && (ch <= '9')) {
      /* ch is a digit */
    }
    but there is a perfectly good function, isdigit(), that does all this
    for us so why bother? Speed is actually the only reason I can
    think of not to use isdigit() - but there are probably better ways
    to speed up your program!
    Last edited by DavT; 03-16-2004 at 03:36 AM.
    DavT
    -----------------------------------------------

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A lot of people have been asking about isdigit recently. Just search the boards, I'm quite sure you question has already been answered in the past two days or so.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 09:18 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM