I like this code:
Code:
#include <stdio.h>
#define SIZE 512
int main(void) {
printf("%s\n", "Enter a float");
char buffer[SIZE];
fgets(buffer, SIZE, stdin);
double value;
while (sscanf(buffer, "%lf", &value) != 1) {
puts("Invalid entry! Try Again!");
fgets(buffer, SIZE, stdin);
}
printf("You entered %lf\n", value);
}
or a minus value was entered.
This code I wrote could be adapted to only take positive by changing value and sscanf to take unsigned instead of signed.
(There was three posts while I wrote this.)