| | |
Read an Integer from the User, Part 3
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
#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 |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi



