Read an Integer from the User, Part 3

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Dave Sinkula Dave Sinkula is offline Offline Dec 22nd, 2005, 1:20 pm |
0
This is a strtol version of Read an Integer from the User, Part 2.
Quick reply to this message  
C Syntax
  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. */

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC