From 1f9785e9c8accae60bdcd74b0bf83a171bcf6c5e Mon Sep 17 00:00:00 2001 From: "tim.schubert@tu-bs.de" Date: Wed, 26 Oct 2016 22:28:04 +0200 Subject: [PATCH] Add error message for illegal input. --- netcalc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/netcalc.c b/netcalc.c index 4a3a310..7cd7128 100644 --- a/netcalc.c +++ b/netcalc.c @@ -206,14 +206,18 @@ int server() continue; } + memset(buf, 0, sizeof buf); + unsigned int result; if (calc(first, second, op, &result) == -1) { - fprintf(stderr, "calc: invalid operator %c for %u and %u", op, first, second); - continue; + char *errormsg = "error: %u %c %u invalid\n"; + fprintf(stderr, errormsg, op, first, second); + sprintf(buf, errormsg, first, op, second); + } else { + printf("%u %c %u = %u\n", first, op, second, result); + sprintf(buf, "%u\n", result); + } - memset(buf, 0, sizeof buf); - printf("%u %c %u = %u\n", first, op, second, result); - sprintf(buf, "%u\n", result); if (send(c_fd, buf, sizeof buf, 0) == -1) { perror("send"); continue;