-
C (
http://www.daniweb.com/forums/forum118.html)
| Dave Sinkula | Nov 20th, 2006 11:44 pm | |
| Read a Floating-Point Value from the User, Part 3 This snippet uses strtod to be a little more stringent. |
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int mygetd(double *result)
{
char *end, buff [ 32 ];
return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
(*result = strtod(buff, &end)), ( *end == '\n' || *end == '\0' );
}
int main(void)
{
double value = -12.3;
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
*/
| bemboysms | Jul 30th, 2009 11:11 pm | |
| how about reading a temperature in degree celsius and then conbert it to fahrenheit? |
| All times are GMT -4. The time now is 1:08 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC