Thread: Is there a way to error check against input of the wrong data type?

  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33

    Is there a way to error check against input of the wrong data type?

    I've been working on this exercise in the book "C: How to Program (Global 9th Edition)" where you create a program that accepts a pass and fail grade and then tells you how many passed/failed at the end along with "giving you a bonus" if you have 9 or more passes as the teacher. It's the same exercise that I posted in my last thread about unassigned integers:
    Code:
    #include <stdio.h>
    int main ()
    {
        int passes = 0;
        int failures = 0;
        int student = 1;
    
        while (student <= 10) 
        {
            printf ("Enter 1 for pass, 2 for fail: ");
    
            int result;
    
            scanf ("%d", &result);
    
            while (result != 1 && result != 2)
            {
                printf ("Oops! Enter 1 or 2 or exit with ctrl + c: ");
    
                scanf ("%d", &result);
            }
    
            if (result == 1)
            {
                ++passes;
            }
            else 
            {
                ++failures;
            }
            
            ++student;
        }
        
        printf ("%d students passed,", passes);
        printf (" %d students failed.\n", failures);
    
        if (passes > 8)
        {    
            puts("Congratulations! Instructor gets a bonus!");
        }
    
        return 0;
    }
    I added a mechanism for checking whether the user entered just 1 or 2 like they are supposed to, and tell them how to just exit the program. It works fine, HOWEVER, if you enter a character instead of an integer, the program spits out my error message in an infinite loop.

    Is there a way to repeat the error loop the way it's supposed to be repeated if someone were to enter a letter/char instead of a number? I'd think that in a real world scenario, this would be a pretty grave security vulnerability...that's interesting, but it would also be interesting to test for the wrong data type in the input and output the same message that the user would get with a 0 or 3.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        if ( fgets(buff, BUFSIZ, stdin ) ) {
            char *endp;
            errno = 0;
            long temp = strtol(buff,&endp,0);
            if ( buff == endp ) {
                printf("Not an integer\n");
            }
            else if ( errno != 0 ) {
                printf("Way out of range\n");
            } else {
                // it's a valid integer, is it what we want?
                if ( temp >= 1 && temp <= 2 ) {
                    printf("Congrats!\n");
                    // It's all good, do our thing
                } else {
                    printf("Out of range\n");
                }
            }
        } else {
            printf("EOF, or stdin broke\n");
        }
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    dfgf
    Not an integer
    $ ./a.out 
    EOF, or stdin broke
    $ ./a.out 
    1323435465768797946353
    Way out of range
    $ ./a.out 
    3443
    Out of range
    $ ./a.out 
    2
    Congrats!
    So yeah, you can make it bomb proof, but it takes a lot more code than just calling scanf.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2012, 08:34 AM
  2. Replies: 2
    Last Post: 03-07-2012, 07:16 AM
  3. Check input data
    By Cevris in forum C Programming
    Replies: 17
    Last Post: 05-19-2011, 11:53 AM
  4. input data type
    By QuestionKing in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2009, 06:50 AM

Tags for this Thread