-
C (
http://www.daniweb.com/forums/forum118.html)
| Dave Sinkula | Dec 22nd, 2005 1:20 pm | |
| 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
*/
| All times are GMT -4. The time now is 8:07 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC