Thread: isdigit function help!!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    isdigit function help!!

    I read that isdigit() fn returns a non-zero number, if its argument is a digit(0 to 9) and it returns zero otherwise.....

    but consider the program.....
    Code:
    #include<iostream>
    #include<ctype>
    int main()
    {
    
             int c=3;
             if(isdigit(c))
             {
                    cout<<"good";
             }
             else
             {
                    cout<<"bad";
             }
    }
    The output comes as bad
    why? please help.......

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    sorry . . . i forgot to type "return 0;" there .... now please help.....

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to pass in a character to check if it is a digit (for example '9' is a digit, 'Z' is not).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Yes, isdigit works only for char types. You declared your variable as integer. The ctype.h functions refer to chars only.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The inherited `std::isdigit' works with `int'.

    nvoigt has it right. There is a difference between an integer variable with the integer value of `3' and the "ASCII" character value representing`3'.

    Code:
    int three = 3;
    Code:
    int three = '3'; // correct
    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM