Thread: using isdigit()

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    using isdigit()

    hi!

    i'm trying to check if a string has numbers or chars, for example: char string[3];

    how can i accomplish this using isdigit() or with other method?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    isdigit() and a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use a loop and check each character with isdigit().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    isdigit() checks for digits and isalpha() checks for letters.

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    Code:
    char ipend[3];
    
    size = strlen(ipend);
    
    for(i=0; i<=size; i++)
    {
    	if( isdigit(ipend[i]) )
    	{
    		MessageBox(hWnd, "digit", "digit", MB_OK);
    	}
    	else MessageBox(hWnd, "non digit", "non digit", MB_OK);
    }
    i tried this but it doesn't work properly
    it works fine with the 2 first characters of the string, but it doesn't seem to check the last one correctly.
    "Artificial Intelligence usually beats natural stupidity."

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By the last character, do you mean the \0 terminating character? Is there supposed to be something in ipend already?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That's because strlen() does not count the terminating null byte if its already populated. Use the sizeof operator instead.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > it works fine with the 2 first characters of the string, but it doesn't seem to check the last one correctly.
    char [3] can only hold 2 characters, and a \0.

    Otherwise, you're trashing memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    i tried: size = sizeof(ipend);

    but the it doesn't work neither
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Back to basics, how did you populate your string?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    With the code as it's shown, the string contains whatever garbage was in memory.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    Code:
    char ipend[3];
    		
    size = sizeof(ipend);
    
    for(i=0; i<=size; i++)
    {
    	if( isdigit(ipend[i]) )
    	{
    		MessageBox(hWnd, "digit", "digit", MB_OK);
    	}
    	else MessageBox(hWnd, "non digit", "non digit", MB_OK);
    }
    "Artificial Intelligence usually beats natural stupidity."

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Wrong again. ipend[3] is out of bounds.

    Stop "hacking" at it, learn and do it properly.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    And ipend still contains garbage.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit woes
    By kryonik in forum C Programming
    Replies: 3
    Last Post: 10-14-2005, 05:10 PM
  2. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 06:03 AM
  4. #include cctype and the isdigit
    By nextus in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 07:55 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM