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);