| | |
Read a Floating-Point Value from the User, Part 2
Some issues, such as leading whitespace and trailing characters that cannot be part of a number, were not handled in Read a Floating-Point Value from the User, Part 1. Here such issues receive lip service.
See also Read a Floating-Point Value from the User, Part 3.
See also Read a Floating-Point Value from the User, Part 3.
#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 */
Similar Threads
- Code Snippet: Read an Integer from the User, Part 2 (C)
- how to read a binary file holding floating-point values (VB.NET)
- Code Snippet: Read a Floating-Point Value from the User, Part 1 (C)
- FLOATING POINT.......Please (C++)
- Code Snippet: Read an Integer from the User, Part 1 (C)
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h



