From 50804be2f709bc58f3aecea780a3a052f94d5a1a Mon Sep 17 00:00:00 2001 From: Yurii Chechur Date: Mon, 14 Oct 2024 11:06:09 +0000 Subject: [PATCH] Update cv3/program.c --- cv3/program.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index d6dcdad..6c10487 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -63,12 +63,17 @@ int read(struct stack *s) { } float value; - int scan = sscanf(temp, "%f", &value); + char extra; + int scan = sscanf(temp, "%f %c", &value, &extra); - // If it's a number + // If it's a properly formatted number if (scan == 1) { push_stack(s, value); print_stack(s); + } else if (scan > 1) { + // If extra characters were found, it's an invalid input like .23.4 + printf("bad input\n"); + exit(0); } else { switch (temp[0]) { case '+': @@ -102,13 +107,14 @@ int read(struct stack *s) { } default: printf("bad input\n"); - exit(0); // Continue on invalid input + exit(0); // Exit on invalid input } } return 1; // Continue the loop } + int main() { struct stack my_stack; init(&my_stack);