From 4458eb8af16c97370e84d7a5a508b4bf71758d70 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Mon, 27 Dec 2021 18:24:58 +0800 Subject: 5.1 --- 5/1.c | 10 ++++++++-- 1 file 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'); -- cgit v1.2.3