Type checking

This is a discussion on Type checking within the C Programming forums, part of the General Programming Boards category; Is there anyway that I can check if the input collected by scanf() is of type Interger? eg, If I ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    4

    Type checking

    Is there anyway that I can check if the input collected by scanf() is of type Interger?

    eg, If I enter a number, all is good, and the operation continues.
    but if I enter the letter 'a' or a special character like '?' it will stop the program.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    hi
    grab the input to a char array and then check the content of that array so it holds only a digits. if all is ok then convert it to int using atoi or something similar.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    4
    Thanks, I'll give that a shot.

  4. #4
    Witch_king's puppet
    Join Date
    Aug 2001
    Posts
    20
    You could use something like this:

    Code:
    printf("Enter number: ");
    while(scanf("%d",&num) != 1 || num < 0 || num > 200)
    {
       puts("Out of range");
       while(getchar() != '\n') continue;
    }
    I'm A Farmer

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    4

    Different problem.

    Ack! Thanks for the help, but just realised that wasn't my problem. *sigh*

    How can I test a number so that I can detect if it will cause an operation down the line to result in an undefined number (larger than can be stored in the space reserved for an INT)?

    The problem is that for example, a number like 477218587 will cause such a problem, but numbers like 477218586, 477218588 or 477218589 do not. It isn't based on size nor being odd or even, and there isn't a pattern that I can see.

    The program actually does several loops until a certain number is found. In which case, it returns 'Yes the number works and was completed in 15 iterations', or 'No the number doesn't work' if it overflows beyond max_uint or goes below 0.

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Numbers are truncated if they are larger than the built in type in which they are stored. You need to use validation like in Wan Valdez's example.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    No that's not going to work. I think the only way is to read in the string
    keep it in a string while you check if it's between INT_MIN and INT_MAX then
    convert it.

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Yes it does work. You can use scanf, you don't have to use that damn string bull crap.
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    Ordinarily, I would use fgets to read in a line from the user (or a file), and then use sscanf to convert that number to an integer.

    However, if you're concerned about overflow, then sscanf won't cut it, you would need to use strtod or strtol.

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    
    void one ( char *num ) {
        long r1;
        double r2;
    
        r1 = strtol( num, NULL, 10 );
        if ( errno != 0 ) {
            perror( "Conversion error" );
        } else {
            printf( "Value=%ld\n", r1 );
        }
    
        r2 = strtod( num, NULL );
        if ( errno != 0 ) {
            perror( "Conversion error" );
        } else {
            printf( "Value=%f\n", r2 );
        }
    }
    
    void two ( char *num ) {
        int res, n;
        res = sscanf( num, "%d", &n );
        if ( res == 1 ) {
            printf( "Value=%d\n", n );
        } else {
            printf( "Not a number\n" );
        }
    }
    
    int main ( ) {
        /* use fgets to read in the input from the user */
        /* This number has the value 2 when truncated to 32 bits */
        char num[] = "1311768464867721218";
        one( num );
        two( num );
        return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Or else just know the range of the built in type and use scanf!
    I compile code with:
    Visual Studio.NET beta2

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    4
    Thanks for the help guys,

    I didn't know that I could do something like

    while(scanf("%d",&num) != 1....

    That helped checking for data being entered, then I just cheated for the second bit and casted the unsigned int into a double, and doing the operations with that and checking it against UINT_MAX before changing it back to an int if errors were not encountered.

    Once again, thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  4. Data type checking?
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 05:24 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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