•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,698 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,521 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
This is a
strtol version of Read an Integer from the User, Part 2.
#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 */
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)