From 0c7a59180de778a4e9019176a65eb02b5493b6f7 Mon Sep 17 00:00:00 2001 From: zlg Date: Sat, 19 Dec 2015 18:40:02 -0800 Subject: 1-16 solution code and comment style cleanup Also of note is the length that the program reports includes the newline character at the end. This has been clarified in a comment. The exercise text does not specify whether to include newlines as part of the length, so it was left alone. Removed the contributor notice, since contributor acknowledgements are centralized. --- ch5/5-01_getint-fixed.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'ch5/5-01_getint-fixed.c') diff --git a/ch5/5-01_getint-fixed.c b/ch5/5-01_getint-fixed.c index 8d53ba6..b013f20 100644 --- a/ch5/5-01_getint-fixed.c +++ b/ch5/5-01_getint-fixed.c @@ -23,6 +23,7 @@ int main() { while (getint(bar) > 0) { printf("%d\n", foo); } + printf("Contents of buffer:%s\n", buf); return 0; } @@ -38,8 +39,14 @@ int getint(int *pn) { sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') { c = getch(); + if (!isdigit(c)) { + ungetch(c); + ungetch(sign == 1 ? '+' : '-'); + return 0; + } } if (!isdigit(c)) { + ungetch(c); return 0; } for (*pn = 0; isdigit(c); c = getch()) { -- cgit v1.2.3-54-g00ecf