Thread: isdigit() to validate if input is number..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    isdigit() to validate if input is number..

    Hi..
    i have a doubt..
    i have seen that isdigit() function is used with "character" data type..
    but can we use to validate an "integer"...??
    i mean is it ok..to write like this..

    Code:
    int num;
    scanf("%d",&num);
    if(isdigit(num))
    {
      printf("you entered a number..");
    }
    else
    {
      printf("enter only an integer number..");
    }
    My task is to check the input given by user..whether it is an INTEGER or not..
    Kindly help me..
    thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check the return value of scanf instead. isdigit is meant to determine if a character in a string is a digit. If you really wanted to use it to validate user input, then you should read the input as a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Check the return value of scanf instead.
    I did not get you.on the above statement.

    can you please show me a small program on how to check user input.
    i want the user to tell
    1.if he enters a character..i want to tell him to enter only integers not characters.
    2.if he enters a valid integer..but a negative one..then i have to prompt the user..not to enter negative numbers..
    Thank you.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You pretty much have to read the input as a string (as laserlight says) and then attempt the conversion checking the result and the consumed length of input.

    Soma

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    @@phantomotap..

    A small program PLEASE..!!
    it would help me a lot..!!

    Thank you.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to do this.
    Code:
    if ( scanf("%d",&num) == 1 ) {
        // num was read
    } else {
        // oops
    }
    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.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    but can we use to validate an "integer"...??
    It would take literally one minute to write up a little program to test this question. Have you tried that? I'd guess not, since you're asking.

    My task is to check the input given by user..whether it is an INTEGER or not..
    Read up on the return type of "scanf()" - this will give you insight on how to validate the user's input.

    EDIT: Oops, let the page sit too long! Didn't know everyone else was in on this one.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    this is the program that i wrote ... for adding the digits in a number..
    I USED THE CODE GIVEN BY SALEM TO WRITE THE PROGRAM.
    Somehow it's not working..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int sumofidigits(int);
    
    
    int main()
    {
        int num,sum = 0;
        printf("Enter a non-negative number\n");
        if(scanf("%d",&num) == 1);
        {
          sum = sumofidigits(num);
          printf("Sum of the digits is %d",sum);
        }
       
       return 0;
    }
    int sumofidigits(int x)
    {
        int rem,sum = 0;
        while(x>0)
        {
            rem = x % 10;
            x = x / 10;
            sum = rem + sum;
        }
        return(sum);
    }
    thank you.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have an extra semi-colon in a most unfortunate location.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    @@laserlight ..
    i am sorry.. i was stupid not to notice it..
    thanks for pointing it out.
    But i still dont understand this
    Code:
    if(scanf("%d",&num) == 1)
    Is it the number of successfully read items that are returned by the scanf statement..??

    Thank You
    Last edited by narendrav; 06-16-2012 at 12:12 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The comparison with 1 is to check that scanf did indeed read one item.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    @@laserlight,@@Matticus ,@@Salem,@@phantomotap
    Thanks for your help.
    I have now sorted it out.
    But today i learnt something about "scanf" which i did not know earlier.
    Once again thanks for taking time and explaining everything.

    Thank You.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate input value
    By aladin in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 09:41 AM
  2. Validate double number
    By guaro555 in forum C Programming
    Replies: 38
    Last Post: 01-15-2008, 07:08 AM
  3. Replies: 15
    Last Post: 01-11-2008, 03:57 PM
  4. How to Validate an Input
    By slowcoder in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 07:33 PM
  5. Validate data on input
    By hello_moto in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2006, 04:26 AM