| | |
Read a Floating-Point Value from the User, Part 3
#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 */
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 an Integer from the User, Part 3 (C)
- Code Snippet: Read a Floating-Point Value from the User, Part 2 (C)
- Code Snippet: Read a Floating-Point Value from the User, Part 1 (C)
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h



