-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetint.c
60 lines (50 loc) · 1.04 KB
/
getint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <ctype.h>
/* too lazy to get old code for getch and ungetch,
* instead use built in getchar and ungetc */
void skip_non_digits()
{
char c;
while(!isdigit(c = getchar()) && c != EOF)
;
if (isdigit(c) || c == EOF) {
ungetc(c, stdin);
}
}
/*getint:get next integer from input into *pn */
int getint(int *pn)
{
int c, sign;
while(isspace(c = getchar())) /*skip white space */
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
/* it's not a number */
skip_non_digits();
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c== '+' || c== '-' ) {
c = getchar();
if (!isdigit(c)) {
/* it's not a number */
skip_non_digits();
return 0;
}
}
for (*pn =0; isdigit(c); c = getchar())
*pn = 10 * *pn + (c-'0');
*pn *= sign;
if(c != EOF) {
ungetc(c, stdin);
}
return c;
}
int main()
{
int return_value, converted;
while((return_value = getint(&converted)) != EOF) {
if (return_value) {
printf("-> %8d\n", converted);
}
}
}