diff --git a/chapter05/5-1.c b/chapter05/5-1.c index 96c7552..d4294d2 100644 --- a/chapter05/5-1.c +++ b/chapter05/5-1.c @@ -46,8 +46,10 @@ int getint(int *pn) } sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') - if (!isdigit((c = getch()))) /* sign followed by a digit? */ + if (!isdigit((c = getch()))) { /* sign followed by a digit? */ + ungetch(c); /* push the non-digit character back */ return 0; /* not a number */ + } for (*pn = 0; isdigit(c); c = getch()) *pn = 10 * *pn + (c - '0'); *pn *= sign;