isdigit()

This is a discussion on isdigit() within the C Programming forums, part of the General Programming Boards category; So I have stored a string from the user input, and I want to check whether it is an integer. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    isdigit()

    So I have stored a string from the user input, and I want to check whether it is an integer. So it calls my is_digit function, sending it the inputted sting:
    Code:
     int is_digit(char digit)
        {
            while(1)
            {
    
                if (isdigit(digit))
                {
                    return (digit);
                    break;
                }
                else
                {
                    printf("Please reenter value, only numbers please\n");
                    scanf("%s", digit);
                    printf("%s", digit);
                    continue;
                }
            }
        }
    so it is meant to check whether digit is a number, if it is send it back else ask for a number, check that is a digit, and if se, send it back

    whats wrong with what i wrote above.
    please can someone help

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What's wrong with the concept:
    It seems like a better idea to call isdigit from your original input function, rather than trying to do input and checking in the same function.

    Not strictly wrong, but completely meaningless:
    To continue the loop is implied at the end of the loop, and there is nothing to "skip to" at beyond your continue, so you can remove that.

    Possibly wrong:
    If you have not yet set digit before calling the function, it is incorrect to call isdigit() on the uninitialized value of digit.

    Some things that are DIRECTLY wrong in
    Using the %s format to read a single character will not work - that format reads a string consisting of zero or more characters, terminating with a zero at the end of the string. You are supplying scanf with a single char - the only valid string that could possibly be stored in one char is an empty string - whch is not a digit. I suggest you use %c instead.

    You also do not supply the address of digit, to scanf.

    Also beware of the newline - you may want to put a space in your format string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    if you want to check a char printf or scan do not use %s but %c. %s is for strings(char*),pointers to char.
    If you change %s to %c you check only one char.
    TO check a string use char* type and check every char of the string.a char* is an array of chars.
    Use strlen(mystring) to get the length of your string and then
    use for to check every char of your string like
    Code:
    char userinput[100];
    scanf("%s",userinput);
    len=strlen(userinput);
    
    for(int i=0;i<len;i++)
    {
    if(isdigit(userinput[i])==true)
    ...
    else
    ...
    }
    Last edited by xmariux; 04-12-2009 at 09:01 AM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    I have to scan for 6 integers, all being scanned for as a sting, but need to all be checked to see if they are integers.
    Code:
    printf("A:");
                scanf("%99s", as);
    
                printf("B:");
                scanf("%99s", bs);
    
                printf("C:");
                scanf("%99s", cs);
    
                printf("Please input Xo, Yo and Zo\n");
                printf("\nXo:");
                scanf("%99s", Xos);
                printf("Yo:");
                scanf("%99s", Yos);
                printf("Zo:");
                scanf("%99s", Zos);
    I thought there would be a neater way of checking to see if they are numbers than putting

    Code:
    char userinput[100];
    scanf("%s",userinput);
    len=strlen(userinput);
    
    for(int i=0;i<len;i++)
    {
    if(isdigit(userinput[i])==true)
    ...
    else
    ...
    }
    after each one.

    or not?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    your function could be like this:
    Code:
    int is_digit(char* digit)
        {
             int len=strlen(digit);
             for(int i=0;i<len;i++)
            {
                 if(isdigit(digit[i])==false)
                 {
                       return -1;
                  }
            }
        
         return atoi(digit);
        }
    the function get a char*(a string) and checks every char if is digit. If all the chars are digits will return the atoi of the digit(ascii to int value). if found a char that is not digit will return -1 which will mean for your main programm error(a char of the user's input is not digit).

    From your main program if you get -1 calling is_digit just ask him for input again.It's good practice not to scanf from is_digit because this function sounds only checking and not for gettting user's input
    Last edited by xmariux; 04-12-2009 at 10:37 AM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    in your main program to get 6 times user's input:
    Code:
    int res=0;
    char user[100];
    
    for(int i=1;i<=6;i++)
    {
    printf("for i=%d\n",i);
    scanf("%s",user);
    res=is_digit(user);
    
    if(res==-1)
    {
    printf("error on input\n");
    }
    else
    {
    printf("Ok value is:%d\n",res);
    }
    
    }
    Last edited by xmariux; 04-12-2009 at 10:43 AM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    So the program sends the input value to the function is_digit. On its return it checks to see whether it = -1:
    Code:
     for(i=1;i<=6;i++)
                        {
                            if (i=1)
                            {
                            //scan for number, send to is_digit
    
    
                            }
                            else if (i=2)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=3)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=4)
                            {
                               //scan for number, send to is_digit
                            }
                            else if (i=5)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=6)
                            {
                                //scan for number, send to is_digit
                            }
    
    
    
                    if(result==-1)
                        {
                            printf("One or more entries contained symbols other than numbers. Please re-enter the data\n");
    
                            continue;
                        }
                    else
                        {
                            printf("Ok value is:%d\n",result);
    
    
                        }
    
                    }
    
            }
    But the for loop doesnt increase i by 1 so it still tries to scan and store in the same spot each time.

    also, what if the entered number is a decimal i.e 0.67 or a negative -43

    it wont like that will it?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                            if (i=1)
                            {
                            //scan for number, send to is_digit
    
    
                            }
                            else if (i=2)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=3)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=4)
                            {
                               //scan for number, send to is_digit
                            }
                            else if (i=5)
                            {
                                //scan for number, send to is_digit
                            }
                            else if (i=6)
                            {
                                //scan for number, send to is_digit
                            }
    1. You have mixed up comparison and assignment operator: compare if equal is ==, make it equal is =.
    2. The point of a loop here is that you should be able to walk through your string [which is an array] and pass the current position to isdigit. You should not have a for-loop and then do if(i == x) for every value of the loop - that's completely pointless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    My example works only for integers >= 0. if the input number is negative the is_digit will return -1 because of the "-".

    It depencne what u want to do and what the program expect from user, u can use convertion functions.

    atoi() wiil convert an char* to int - > example atoi("123abcf") will return 123
    atoi("absd1234") will return 0

    I never tested but i think that atoi will return a negative int if you write something like this atoi("-100")

    So, u can use the concertions and then u can check if the (converted) input is that that your program expects
    Last edited by xmariux; 04-14-2009 at 04:39 AM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    strtol give even more feedback
    if you pass "123abc" to it, it returns 123 and sets the end pointer to point to "abc",

    so checking that something left in the input buffer after converted number you can be sure that the whole buffer is correct or not
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    OK, so i am ammending the function is_digit to allow negative sign as the first symbol, and decimal signs.

    Below is just for the decimal sign, but it doesnt work,

    Code:
    int is_digit(char* digit)
        {
             int len=strlen(digit);
             int i;
             for(i=0;i<len;i++)
            {
                 if(isdigit(digit[i])==0)
                 {
                     if ((digit[i]) == ".")
                     {
                        
                     }
                     else
                     {
                       return -1;
                     }
                  }
            }
    
         return atoi(digit);
        }
    can anyone see why?

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, 08:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 05:03 AM
  4. #include cctype and the isdigit
    By nextus in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 06:55 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM

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