Ex24 Macro does not return error when no data is entered

I was working on ex24 and paying attention to the “check” macro in particular.
Lines 33-35 in Exercise 24.

When the program prompted me to enter my last name, I simply hit the enter key. I assumed it would result in an error.
It did not. The program went on to prompt me for my age.
In troubleshooting this issue I discovered that char code 10 is entered (new line) and EOF(by stdin).

If this line is executed, ( in = fgets(you.last_name, MAX_DATA-1, stdin); ) there seems to be no way to get the program to return NULL and print out the error, since it will return char code 10 when the enter key is pressed.

Am I correct or have I missed something?

Thanks for your assistance,

Below is the output from my console:

(base) C:\MinGW\C-Code>ex24
what’s your First Name?John  I typed my name and then hit the enter key
what’s your Last Name?  I hit the enter key here. No input
len Last Name = 1 10 -1 -Note, strlen = 1, char code = 10, EOF = -1
How old are you? ………. I terminated the program here.

Ex24 code


#define MAX_DATA 12

typedef enum EyeColor{
                             BLUE_EYES, GREEN_EYES, BROWN_EYES,
                             BLACK_EYES, OTHER_EYES

const char *EYE_COLOR_NAMES[] = {
              "Blue", "Green", "Brown", "Black", "Other"

typedef struct Person{         
              int age;
              char first_name[MAX_DATA];
              char last_name[MAX_DATA];
              EyeColor eyes;
              float income;

int main(int argc, char *argv[])
              Person you = {.age = 0 };  // initialize age in the struct 'Person'
              int i = 0;
              char *in = NULL; 
              printf("what's your First Name?");
              in = fgets(you.first_name, MAX_DATA-1, stdin);
              check(in != NULL, "Failed to read First Name1.");              
              printf("what's your Last Name?");
              in = fgets(you.last_name, MAX_DATA-1, stdin);
              check(in!=NULL, "Failed to read Last Name.");
              printf("len Last Name = %d  %d  %d \n", strlen(in), *in, EOF);      ******** I added this line to troubleshoot the issue  ********************
              printf("How old are you?");
              int rc = fscanf(stdin, "%d", &you.age);
              check(rc > 0, "You have to enter a number.");
              printf("What color are your eyes:\n");
              for(i=0; i<= OTHER_EYES; i++){
                             printf("%d) %s\n", i+1, EYE_COLOR_NAMES[i]);
              int eyes = -1;
              rc = fscanf(stdin, "%d", &eyes);
              check(rc > 0, "You have to enter a number.");
              you.eyes = eyes-1;
              check(you.eyes<= OTHER_EYES
                             &&you.eyes >= 0, "Do it right, that's not an option.");
                             printf("How much do you make and hour?");
                             rc = fscanf(stdin, "%f", &you.income);
                             check(rc>0, "Enter a floating point number.");
                             printf("First Name: %s", you.first_name);
                             printf("Last Name: %s\n", you.last_name);
                             printf("Age: %d\n", you.age);
                             printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
                             printf("Income: %f\n", you.income);
                             return 0;
                             return -1;

Well fgets only returns NULL when it fails, which it obviously doesn’t in this case.

You could expand the error condition, e.g. check that the string has a length > 0.

A free service run by Zed A. Shaw for learncodethehardway.org.