Build a calculator with C++

Handling division by zero

Since the user is free to type anything into the console window, let’s make sure the calculator handles any input as expected. Instead of running the program, let’s debug it instead, so we can inspect what it’s doing in detail. Set a breakpoint on the “result = c.Calculate” line, just after the user was asked for input. To do this, click to the left of the line so a red dot appears.

Setting a breakpoint

Now when we debug the program, it will always pause execution at that line. But we already have a rough idea that the program will work for simple cases, and we don’t want to pause execution every time, so let’s make the breakpoint conditional. Right-click the breakpoint, and choose Conditions. In the box on the far right, type “(y == 0) && (oper == ‘/’)” (without quotes).

Conditional Breakpoint

Now we are specifically pausing execution if a division by 0 is being attempted. To debug the program, press F5 or click the Local Windows Debugger button (with the green arrow icon). If you type something like “5-0”, the program behaves as normal and keeps running. But if you type “10/0” (without quotes), it will pause at the breakpoint.

Debugging a conditional breakpoint

Take a look at the Autos window that appears while in debugging mode at the bottom of the IDE. The Autos window shows you the current values of variables used at least 3 lines before the current line and up to the current line. To see all of the variables from that function, switch to the Locals window. You can actually modify the values of these variables on the fly while in Debug mode, to see what effect they have on the program. But in this case, we’ll leave them alone.

Autos Window
Locals Window

You can also just hover over the variables to see their current values where the execution is currently paused.

Hover Tooltip

To move forward one line in the execution of the program, press F10. This is also known as Step Over. You can use Step Over to move from line to line, without delving into the details of what is occurring in each part of the line.

The next line actually calls Calculate, so press F11 next to Step Into the function, rather than stepping over to the cout line directly. You’ll find yourself in the body of the Calculate function.** Step Over** each line with F10 - this continues the execution to the next line below, without going into any implementation details on each line.

It looks like the program is doing what is expected – taking the first number, and dividing it by the second. When you get back to the main() function and hit the cout statement, hover over the “result” variable or take a look at the Autos window, and you’ll see its value is listed as “inf”.

Undefined Value

The cout line will just output whatever value is stored in “result”, so when you step one more line forward, the console window will display this:

Divide by Zero

Let’s handle division by zero more gracefully. Make the following changes in CalculatorTutorial.cpp (you can keep the program running as you edit):

// CalculatorTutorial.cpp : Defines the entry point for the console application. 

#include "stdafx.h"
#include <iostream>  
#include "Calculator.h"

using namespace std;  
int main()  
    double x = 0.0
    double y = 0.0 
    double result = 0.0
    <span style='color:#0000FF;'cahr</span> oper = '+';

    cout << "Calculator Console Application" << endl << endl;   
    cout << "Please enter the operation to perform. Format: a+b | a-b | a*b | a/b" << endl;
    Calculator c;
    while (true)
        cin  >> x  >> oper  >> y;
        if (oper == '/' && y == 0)
            cout << "Division by 0 exception" << endl;
            result = c.Calculate(x, oper, y);
        cout << "Result is: " << result << endl;

    return 0;  

Now press F5 to keep executing the program all the way until it’s time to ask the user for a new operation to run. Type “10/0” again. Now, a more helpful message is printed. The user is asked for more input, and the program continues executing normally.

FinalVerification of Divide by Zero

I ran into an issue Continue tutorial