#include <stdio.h>
#include <ctype.h>

int mygetd(double *result)
{
   char c, buff [ 32 ];
   return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
   sscanf(buff, "%lf%c", result, &c) == 2 && (c == '\n' || c == '\0');
}

int main(void)
{
   double value;
   do
   {
      fputs("Enter a floating-point number: ", stdout);
      fflush(stdout);
   } while ( !mygetd(&value) );
   printf("value = %g\n", value);
   return 0;
}

/* my output
Enter a floating-point number: one
Enter a floating-point number:
Enter a floating-point number: f12.3
Enter a floating-point number: -45.67
value = -45.67

Enter a floating-point number: -12.3f
Enter a floating-point number:    125 help
Enter a floating-point number: 1.2.3
Enter a floating-point number: 1.23
value = 1.23
*/
1
Contributor
0
Replies
1
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.