Thread: isdigit

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    isdigit

    Hey guys just a question related to the function isdigit() how come if i wish to check if its not a digit it wont work work. Even if i type in an integer it still says that bad input is entered i only want it to print the error if they type in any other character except for integers.


    eg

    Code:
    if((!isdigit(h))
    {
         printf("bad input: integers only!\n");
         return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Show some more code, like where you declare h and where you ask for a get the characters from the user.

    And this...
    Code:
    if((!isdigit(h))
    {
         printf("bad input: integers only!\n");
         return 0;
    }
    ... has one to many parenthesis.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably because you're trying to use isdigit() on the converted result, not a character in the input string.

    char buff[] = "123";
    int h = strtol( buff, NULL, 0 );

    if ( isdigit( buff[0] ) )
    Would be OK to call before strtol().
    But so would calling isdigit( buff[1] ) and isdigit( buff[2] ) - in other words, a LOOP.

    if ( isdigit(h) )
    Isn't going to work unless by some small chance you typed in a number between 48 and 56.
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Even if i type in an integer it still says that bad input is entered i only want it to print the error if they type in any other character except for integers.
    How are you parsing the numbers from the input? Most functions that do this (scanf(), strtol(), etc) have a return value or an error state of some sort that you can check for. Here's a scanf() example.
    Code:
    int x, c;
    
    printf("Enter a number: ");
    while(scanf("%d", &x) != 1) {
        while((c = getchar()) != '\n' && c != EOF);  /* flush the input buffer */
        printf("Invalid number. Try again: ");
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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