Skip to Content
Course content

22: Practice Exercise: Building a Simple Calculator

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

Before we dive into the full build, I want to show you a snippet of code I saw a junior dev write a few years ago. They were building a simple calculator, and they were pulling their hair out because their division was "broken."

double result;
int a = 5;
int b = 2;
char op = '/';

if (op == '/') {
    result = a / b; 
}

std::cout << "Result: " << result; // Expected 2.5, but outputs 2

The Case of the Disappearing Decimals

At first glance, this looks fine. The result variable is a double, so it should be able to hold 2.5. But here is the catch: C++ evaluates the right side of the assignment first. Since both a and b are integers, C++ performs integer division. It calculates 5 / 2, drops the remainder, and gets 2. Only then does it convert that 2 into a double to store it in result. By the time the value reaches your variable, the precision is already gone.

Switching to Floating Point

To fix this, you need at least one of the operands to be a floating-point type. I usually recommend just using double for all numeric inputs in a calculator. It simplifies everything and avoids the need for messy casting. If you absolutely must use integers for the input, you'd have to explicitly cast one of them: result = static_cast<double>(a) / b;.

For our calculator project, we'll stick with double. It's cleaner and it's what your users actually expect when they're doing math.

Handling the Operator Logic

When you're building this, you'll likely be tempted to use a long string of if/else if statements. While that works, this is the perfect scenario for a switch statement. It's more readable and generally more efficient when you're checking a single character against multiple constants.

Just be careful with the break keywords. I've seen countless bugs where a developer forgets a break, and the code "falls through" to the next case, performing two different operations on the same set of numbers. It's a frustrating bug to track down if your calculator is large, so double-check your braces and breaks.

Guard-railing Against Zero

Finally, don't forget that computers hate dividing by zero. If you don't check for this, your program might crash or return inf (infinity), which isn't a great user experience. A simple if (b == 0) check inside your division case is all you need to keep the program stable.




📋 Practical Task

Build a Persistent Multi-Operation Calculator

Your task is to create a calculator program that doesn't just perform one calculation and quit. It should stay open, allowing the user to perform multiple calculations until they explicitly choose to exit.

Requirements:

  • Data Types: Use double for all numeric inputs to ensure decimal precision.
  • Operations: Support addition (+), subtraction (-), multiplication (*), and division (/).
  • The Loop: Wrap the logic in a while or do-while loop. The program should ask the user if they want to perform another calculation (e.g., "Press 'q' to quit or any other key to continue").
  • Error Handling: Implement a check to prevent division by zero. If the user attempts it, print a clear error message and let them try again without crashing the program.
  • Control Flow: Use a switch statement to handle the operator selection.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.