Skip to Content
Course content

26: Practice Exercise: Building a Simple Calculator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing code for a junior developer who had built a basic calculator for a utility tool. It worked perfectly—until he tried to divide by zero during a live demo for his lead. The program didn't just give a wrong answer; it crashed instantly with a segmentation fault or a floating-point exception, depending on the environment. He had focused so much on the math that he forgot that in C, the language doesn't "save" you from the hardware. It just executes the instruction, and if the hardware says "you can't do that," the OS kills your process. It was a humbling moment for him, but it's the perfect lesson in why input validation is just as important as the logic itself.

Mapping Operations to Logic

When you're building a calculator, you'll be tempted to write a long chain of if...else if statements to check which operator the user entered. While that works, it's clunky. Since we're dealing with a single char for the operator (like '+', '-', '*', '/'), a switch statement is the cleaner, more professional choice. It tells anyone reading your code, "I am choosing one path based on this specific value."

One thing I want you to watch out for is how scanf handles characters. If you've used it for integers, you know it's straightforward. But when you read a char after reading a number, scanf often picks up the newline character \n left in the buffer from when the user pressed Enter. To avoid this, put a leading space in your format string: scanf(" %c", &operator);. That tiny space tells C to skip any whitespace characters, saving you from a bug that would otherwise make your program skip the operator input entirely.

Defending Against the Crash

As we saw in my anecdote, division is the danger zone. In C, dividing a floating-point number by zero might result in inf (infinity), but dividing an integer by zero is an immediate crash. Even with floats, letting a user divide by zero is poor form. You need to wrap your division logic in a guard clause.

if (operator == '/') {
    if (num2 == 0) {
        printf("Error: Division by zero is undefined.\n");
    } else {
        printf("Result: %.2f\n", num1 / num2);
    }
}

By checking the divisor before the operation happens, you move from writing a "happy path" program to writing robust software. I always tell my students: assume the user is actively trying to break your code. If you design for the worst-case scenario, the best-case scenario takes care of itself.

Keeping the Session Alive

Running a program, performing one calculation, and having it terminate is annoying for the user. To make this a real tool, you'll want to wrap the entire logic in a do...while loop. This allows the user to perform multiple calculations in one session. You can prompt them with a simple "Do you want to continue? (y/n)" at the end of each cycle. Just remember to handle that final character input with the same whitespace awareness we discussed earlier.




📋 Practical Task

Project: The Robust Arithmetic Console

Build a command-line calculator that allows a user to perform basic arithmetic without restarting the program. Your implementation must meet the following requirements:

  • Continuous Loop: Use a do...while loop so the user can perform multiple calculations until they choose to exit by entering 'n'.
  • Flexible Input: Use float or double for the numbers to ensure the calculator can handle decimals.
  • Operator Handling: Implement a switch statement to handle addition (+), subtraction (-), multiplication (*), and division (/).
  • The Safety Guard: Explicitly check for division by zero and print a clear error message instead of allowing the program to crash.
  • Buffer Management: Ensure that your scanf calls for characters do not accidentally consume newline characters from previous inputs.

Expected behavior example:
Enter first number: 10
Enter operator (+, -, *, /): /
Enter second number: 0
Error: Division by zero is undefined.
Would you like to perform another calculation? (y/n): y

Rating
0 0

There are no comments for now.

to be the first to leave a comment.