User Name Password Register
DaniWeb IT Discussion Community
All
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:
Dec 22nd, 2005
Views: 2,364
c Syntax | 4 stars
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. int mygeti(int *result)
  6. {
  7. char *end, buff [ 13 ];
  8. fgets(buff, sizeof buff, stdin);
  9. *result = strtol(buff, &end, 10);
  10. return !isspace(*buff) && end != buff && (*end == '\n' || *end == '\0');
  11. }
  12.  
  13. int main(void)
  14. {
  15. int value;
  16. do
  17. {
  18. fputs("Enter an integer: ", stdout);
  19. fflush(stdout);
  20. } while ( !mygeti(&value) );
  21. printf("value = %d\n", value);
  22. return 0;
  23. }
  24.  
  25. /* my output
  26. Enter an integer: one
  27. Enter an integer:
  28. Enter an integer: f123
  29. Enter an integer: 123f
  30. Enter an integer: 123
  31. Enter an integer: 123
  32. Enter an integer: 1.23
  33. Enter an integer: -42
  34. value = -42
  35. */
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC