C语言简易计算器优化需求:输入错误运算符后返回初始界面
Hey there! Nice job putting together your first C calculator—getting a working program up and running is a huge win. Let's tackle that issue where entering a bad operator exits the program instead of letting the user try again.
The core solution here is to use a loop to repeat the operator prompt until the user enters a valid one. We'll use a do-while loop (perfect for cases where you want to run code at least once) and fix a common input buffer quirk that can mess up repeated scanf calls.
Here's the revised code with explanations of key changes:
#include<stdio.h> int main() { char operator; double firstNumber, secondNumber; // Wrap the operator input and processing in a do-while loop do { printf("Enter an operator (+, -, *, /): \n"); // The space before %c skips leftover whitespace (like newlines) from previous inputs scanf(" %c", &operator); switch(operator) { case '+': printf("Enter two numbers: "); scanf("%lf %lf",&firstNumber, &secondNumber); printf("%.2f + %.2f = %.2f\n",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf("Enter two numbers: "); scanf("%lf %lf",&firstNumber, &secondNumber); printf("%.2f - %.2f = %.2f\n",firstNumber, secondNumber, firstNumber - secondNumber); break; case '*': printf("Enter two numbers: "); scanf("%lf %lf",&firstNumber, &secondNumber); printf("%.2f * %.2f = %.2f\n",firstNumber, secondNumber, firstNumber * secondNumber); break; case '/': printf("Enter two numbers: "); scanf("%lf %lf",&firstNumber, &secondNumber); // Added a check to prevent division by zero (a common edge case!) if(secondNumber == 0) { printf("Error! Cannot divide by zero. Please try again.\n"); // Skip the rest of the switch and go back to operator input continue; } printf("%.2f / %.2f = %.2f\n",firstNumber, secondNumber, firstNumber / secondNumber); break; default: printf("Error! Operator is not correct. Please try again.\n"); // Keep the loop going to ask for a new operator continue; } // If we reach here, a valid operator was processed—exit the loop break; } while(1); // This creates an infinite loop until we hit the 'break' above return 0; }
Key Changes Explained:
do-while(1)Loop: This ensures the operator prompt runs at least once, and keeps repeating until we explicitlybreakout of the loop (after a valid calculation).scanf(" %c", &operator): The space before%ctellsscanfto ignore any whitespace characters (like the newline you press after entering input). Without this, the program would read the leftover newline as an invalid operator on the next loop iteration—super annoying, but easy to fix!continuein Default Case: When an invalid operator is entered,continuesends us back to the start of the loop immediately to ask for a new operator.- Division by Zero Check: Added a quick check to handle division by zero, which is a good habit to build early on (it prevents weird program behavior or crashes).
If you want to take it a step further, you could even add a prompt to let the user run another calculation after finishing one—just wrap the entire logic in another loop!
内容的提问来源于stack exchange,提问作者Sagewizard88

