#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Obtaining integer rather then char


    i want to obtain a long integer, but i want to make sure the user didn't type in a char instead.
    how can i check that?

    thanks in advanced!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    #include <ctype.h>

    then check the characters individually with isdigit(character)

    ctype.h
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803

    Thumbs up


    One way:

    Code:
    long x = 0;
    int check = 0 ;
    
    ...
    
    do
    {
        printf( "Enter number: " ) ;
        check = scanf( "%ld", &x ) ;
    
    } while( check == 0 ) ;
    while( getchar() != '\n' ){}
    The do-while loop will repeatedly ask for input until something that can be interpreted as a decimal integer is entered. The empty while-loop, removes the newline and any other 'junk' entered after the decimal.

    This is quick and simple but may not exactly meet your requirements. If for example a user entered:

    1234ABCD

    x would have the value 1234 and the ABCD would be discarded. If you want more strict input validation, then you would have to do that yourself using b49P23TIvg's suggestion for example:

    Code:
    #include <stdbool.h>
    ...
    
    char input[16] ;
    int i ;
    long x ;
    
    ...
    
    do
    {
        bool valid = true ;
        printf( "Enter number: " ) ;
        fgets( input, sizeof(input), stdin ) ;
        for( i = 0; input[i] != '\0' && input[i] != '\n' && valid; i++ )
        { 
            valid = isdigit( input[i] ) ;    
        }
    } while( !valid ) ;
    sscanf( input, "%ld", &x ) ;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    thanks guys!

    actually im not allowed to use any library besides <stdio.h> (thats the exercise requirement), so i'll just use the first way cliiford suggested.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    stdio.h isn't a library, it declares, thereby providing an interface to, some part of the standard c library.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    so i'm not allowed to import anything but stdio.h :D
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    ok, so i used clifford way (i was able to obtain string, but thats good enough), and there seems to be something wrong, but i can't figure out what...:

    Code:
    #include<stdio.h>
    
    int main()
    {
      char c;
    
       printf("Enter a number...\n");
       do
       {
        c=getchar();
        if (!(isdigit(c)))
           printf("wrong Input!\n");
       } while (c!='\n');
       return 0;
    }
    so everything is working fine, except...

    these are the outputs for this code:

    Code:
    4564230
    wrong input!
    
    fgh
    wrong input!
    wrong input!
    wrong input!
    wrong input!
    
    443
    wrong input!
    
    E+
    wrong input!
    wrong input!
    wrong input!
    it seems like the first time the program enters the loop, it prints "wrong input!" no matter what.

    why is this happening and is there a way to prevent it?

    thanks in advanced!

    edit...

    OK... i found it... it's not the first time it enters the loop but the last time, when 'c' holds '\n'... :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Please crank up the warning level of your compiler, and mind the warnings. It should have complained about using a function without a prototype, namely isdigit(). The prototype for the function is in the header <ctype.h>.

IMN logo majestic logo threadwatch logo seochat tools logo