Thread: #include cctype and the isdigit

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question #include cctype and the isdigit

    i am having a problem with the function isdigit from the library cctype. isdigit is suppose to return true if the agrument in isdigit is a digit between 0-9. but it keeps returning 0...
    here is my code...help is appreciated.

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(void)
    {
    
    	int x = isdigit(5);
    	cout << x;
    	return 0;
    }
    thanks..
    nextus, the samurai warrior

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try something like this...
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(void)
    {
      char x;
    
      cout << "Enter Something: ";
      cin >> x;
    
      cout << isdigit( (int)x ) << endl;
    	
      return 0;
    }
    "...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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int x = isdigit(5);
    In this case you are looking at the decimal value of a character, not a 5 as you would type on your keyboard.

    Try this instead:
    >>int x = isdigit('5');

    If you don't understand, have a look at the ASCII Chart. You'll see that 5 is an ENQ, whereas '5', which equates to a 5 on your keyboard (general terms), is actually a decimal 53 in ASCII.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed