| | |
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 arrays binarysearch calculate centimeter char character 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 highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h



