summaryrefslogtreecommitdiffstats
path: root/5/1.c
diff options
context:
space:
mode:
Diffstat (limited to '5/1.c')
-rw-r--r--5/1.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/5/1.c b/5/1.c
index 19e4bf2..7f34260 100644
--- a/5/1.c
+++ b/5/1.c
@@ -5,12 +5,16 @@
int getint(int *);
+/* Converts input to integer using a pointer to a variable */
int main(int argc, char *argv[]) {
int rc, n;
while ((rc = getint(&n)) != EOF && rc != 0)
printf("\t%d\n", n);
+ if (rc == 0)
+ printf("error: not an integer\n");
+
return 0;
}
@@ -39,8 +43,10 @@ int getint(int *pn) {
sign = (c == '-') ? -1 : 1;
- if (c == '+' || c == '-')
- c = getch();
+ if ((c == '+' || c == '-') && !isdigit(c = getch())) {
+ ungetch(c);
+ return 0;
+ }
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');