diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-27 18:24:58 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-27 18:24:58 +0800 |
| commit | 4458eb8af16c97370e84d7a5a508b4bf71758d70 (patch) | |
| tree | ea2e6962209c156dde2ea6110ec10a9aecc3b68a /5/1.c | |
| parent | 17dfe130ef58ed790d46b01637b1a12afce456bd (diff) | |
| download | k&r-exercises-4458eb8af16c97370e84d7a5a508b4bf71758d70.tar.gz | |
5.1
Diffstat (limited to '5/1.c')
| -rw-r--r-- | 5/1.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -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'); |
