0

I will try to explain the issue here. I have written this code that accepts various types of inputs:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main()
{   
int number;
printf("press <ENTER> to continue...");
while( getchar() != '\n' );

char *p, s[100];
int n=0;


printf("enter a number: ");
while (fgets(s, sizeof(s), stdin))
{
    n = strtol(s, &p, 10);
   if (p == s || *p != '\n')
        {
        printf("Invalid integer, please try again: ");
        }
    else
        break;
}

printf("You entered: %d\n", n);
printf("Enter an integer between 10 and 20: ");
scanf("%d", &number);
while (1)
{
    if (number < 10 || number > 20)
    {
    printf("Invalid value, 10 < value < 20: ");
    scanf("%d", &number);
    }
    else
    {
        break;
    }
}
printf("You entered: %d\n", number);

//part 3 
double decpart;
printf("Enter a floating number num: ");
char buf[100];
int len;
char *endptr;

while (1)
{
    fgets(buf,sizeof(buf),stdin);
    len = strlen(buf)-1;

    // right strip spaces (replace by linefeed like fgets ends the input)
    while(len>0)
    {
        len--;
        if (buf[len]==' ')
        {
            buf[len]='\n';
        }
        else
        {
            break;
        }
    }

    double floatnum = strtod(buf,&endptr);

    if (endptr[0]!='\n')
    {
        printf("Invalid floating point number, enter again: ");
    }
    else
    {
        int intpart = (int)floatnum;
        double decpart = floatnum - intpart;
        if (decpart == 0.000000){
            printf("Invalid floating point number, enter again: ");
        }
        else
        {
            printf("Number entered = %.2f\n", floatnum);
            break;
        }
    }
}

double floatnum1;
printf("Enter a floating point number between 10.00 and 20.00: ");
scanf("%lf", &floatnum1);
while (1)
{
    if (floatnum1 < 10.00 || floatnum1 > 20.00)
    {
        printf("Invalid value, 10.000000 < value < 20.000000: ");
        scanf("%lf", &floatnum1);
    }
    else
    {
        break;
    }
}
printf("You entered: %0.2lf\n", floatnum1);
printf("End of tester program for milestone one!\n");

return 0;

}

Problem occurs on Part 3 of this code. I see on screen Enter a floating number num: and immediately without waiting for user input it prints Invalid floating point number, enter again:

This is not the case if I just run part3(commented here in code as //part3) independently, it just works fine.

Any idea, why that is happening?

John
  • 89
  • 2
  • 9

1 Answers1

0

The reason for this behaviour lies in the usage of scanf followed by fgets

scanf reads a number from standard input, and stops as soon as it encounters a non-digit character, which is the newline in this case.

Next fgets reads a whole line. But now there's still the single newline in the input, which satisfies fgets even though this is only an empty line.

When you skip over whitespace and finally check for a newline, endptr only points to a \0 character. Thus the message

Invalid floating point number, enter again:


To fix this, you must first skip whitespace before reading further with fgets.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198