function isdigit() is catching the caracter ''

This is a discussion on function isdigit() is catching the caracter '' within the C Programming forums, part of the General Programming Boards category; Hello! I'm writing a function to read input, and i want to have only numbers, so I used the function ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    74

    function isdigit() is catching the caracter ''

    Hello!
    I'm writing a function to read input, and i want to have only numbers, so I used the function isdigit(). I then tested it out and the caracter '' somehow seems to be considered a digit.

    The code I have here is not the one I'm writing, is just a test to see if i can correct it. I wrote an if statement to get rid of the '' caracter but it doesn't work, and i can't see why.

    Hope you can help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    char string[20];
    int stringlenth = 0;
    
    
    for ( ; ; )
    {
        string[stringlenth] = getchar();
    
        if (string[stringlenth] == '\n')
            {
            string[stringlenth] = '\0';
            break;
            }
    
        if (isdigit(string[stringlenth]) != 0)
            {
            if (string[stringlenth] == '') //this should get rid of the caracter... but it doesn't
                {
                continue;
                }
            printf ("%c" , string[stringlenth]);
            ++stringlenth;
            }
    
    }
        return 0;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Without accounting for wide characters, your program will not deal well with things like ''. If you want to test for input like that, see if you can incorporate <wctype.h> as opposed to <ctype.h>.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    74
    didn't know that.
    I've just test it out and worked. thanks kermit!

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, 02:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01: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, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21