Why is Char Data Type used in this Function?

Viewed 122

I'm a student and was given this function by our teacher to get user input. It's used for integers but I'm confused why there's a char data type. If someone could explain this function that would be great.

#pragma warning (disable: 4996)
int getNum(void)
{
    char record[121] = {0};
    int number = 0;
    fgets(record, 121, stdin);
    if( sscanf(record, "%d", &number) != 1)
    {
        number = -1;
    }
    return number;
}

When calling we do this for example:

    int age = 0;      //we always have to initialize
    age = getNum();   //this is how we call the function to get user input
3 Answers

The char record[121] variable is actually an array of characters, otherwise known as a string. It is used here so that, if an erroneous input is given (instead of a valid integer input), then the input stream is still cleared by the fgets() call, which takes in all characters up to (and including) the newline character.

The sscanf function then attempts to extract a valid integer from the read character string and, if it fails, then an error value (-1) is assigned to signal that fact. If it succeeds, it assigns the given input value to number, in the same way that a successful call to scanf("%d", &number) would.

However, just using such a simple scanf() call would potentially leave 'unprocessed' characters in the input stream, which could cause problems for any subsequent input operations (or require the input stream to be cleared).


Using such a technique will also prevent undesired/unexpected effects caused by a user typing extra characters after a valid input. For example, the following (deliberately bad) code will cause a 'surprise' if you enter 1a in response to the first prompt:

#include <stdio.h>

int main()
{
    int number = 0, number2 = 0;
    printf("Enter first number: ");
    scanf("%d", &number);
    printf("Enter second number: ");
    scanf("%d", &number2);
    printf("Given numbers were: %d and %d.\n", number, number2);
    return 0;
}

However, using the getNum() function your teacher provided, the following code is much more robust:

int main()
{
    int number = 0, number2 = 0;
    printf("Enter first number: ");
    number = getNum();
    printf("Enter second number: ");
    number2 = getNum();
    printf("Given numbers were: %d and %d.\n", number, number2);
    return 0;
}

The program is reading lines from a file with fgets, which uses a string as storage buffer for the line. After that it processes the line to find a string that can be read as an int (%d), checks if that operation went OK, and returns the integer if so.

The function is reading input as text, storing it to the character array record. It then converts that string into an equivalent integer value using sscanf.

This is a bit safer than just using scanf directly - it ensures that if the user types non-numeric input, it's still consumed from the input stream so that it doesn't cause problems later on.

Related