| | |
Read an Integer from the User, Part 3
#include <stdio.h> #include <stdlib.h> #include <ctype.h> int mygeti(int *result) { char *end, buff [ 13 ]; fgets(buff, sizeof buff, stdin); *result = strtol(buff, &end, 10); return !isspace(*buff) && end != buff && (*end == '\n' || *end == '\0'); } int main(void) { int value; do { fputs("Enter an integer: ", stdout); fflush(stdout); } while ( !mygeti(&value) ); printf("value = %d\n", value); return 0; } /* my output Enter an integer: one Enter an integer: Enter an integer: f123 Enter an integer: 123f Enter an integer: 123 Enter an integer: 123 Enter an integer: 1.23 Enter an integer: -42 value = -42 */
Similar Threads
- Code Snippet: Read an Integer from the User, Part 2 (C)
- Comparing Python and C, Part 1, Integer Variables (Python)
- 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)
- Code Snippet: Read an Integer 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 givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest 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 reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h



