How to Make a Calculator in C++
By: Nate Metens
Tags: C Programming, Code, Computer Science, Calculator, Programming

I have been coding for almost one year now. I am truly at the gates of my programming career, yet I have learned many cool tricks and I wish to share one with you today.

What is fascinating about coding is that you can make a computer do simple tasks extremely fast and with simple code. Today I would like to show you how to make a simple calculator with the simple options of addition, subtraction, multiplication, and division.

I will create each one of these operations as functions, and implement them into a switch table which will also help with the menu for the calculator. I will be coding this calculator in the language C++. Let us get started…

The first step in creating any program is to #include the header file <iostream>, which allows for the use of cout and cin, the standard input/output stream. Next we may also type “using namespace std;” which saves us from constantly typing std:: before each cout and cin.

Our program now looks like this:

Image 1

Then we can create our main function. Which may look like so:

Image 2

Once we have a main function, we should ask the user which calculation they would like to make. We can give the options “Addition”, “Subtraction”, “Multiplication”, and “Division” as a menu with numbers associated with them from 1 to 4. So something like this:

Menu:

Addition Subtraction Multiplication Division

This will make our use of the switch table much simpler. We can present this in many ways. I like to use arrays because they are fun.

I will create an array of type string and initialize 4 elements, which are the names of each option. Also, I should probably add a message before displaying the array that prints the message “Menu: ”.

Image 3

Then, I wish to display the menu just like I have shown previously. The best way to do this in my opinion is to use a for loop. So I will create one and make it so that it prints each element of the array, starting at element Menu[0] (which is “Addition”) and ending with Menu[4] (which is “Division”). To do this, I will use a counter “i”, which will begin at zero and increment by one until it reaches the fourth element of the Menu string array:

Image 4

So far, our output looks like this:

Image 5

Perfect. Now we can create each of our calculator functions. To do this, we should give each function a name, a type, and a return value. I will explain the first function, then I will follow the same process for the rest.

The first function will be named “Addition” and will be of integer type. Let us take two values: integer x and integer y as input. Then we may simply return x+y, and that’s our function. Also, functions should be created outside of the main function. Here is what all the functions can look like:

Image 6

We should also ask the user which option they would like to pick and then store their decision in a variable to be used for our switch statement:

Image 7

Awesome. Furthermore, we wish to create a switch table that will be our calculator. To do this, we simply create a menu option variable and then four cases (one for each menu option). In each case, we may call our functions appropriately and then break after each one so that we don’t do more calculations than requested. We should also initialize two integer variable “a” and “b” in order to avoid errors (I already added these in the previous image).

Our calculator is almost complete. Next, we simply need to ask the user to provide two integers of their choosing. To do this, we can prompt the user with a cout and then read in the input with a cin. We may do this once and copy and paste it for each case inside the switch statement.

Image 8

We can test out our new calculator code:

Image 9

This code will work, but it is not perfect. We have not taken into account any possible errors on the user’s side, such as providing more or less than two values to be calculated, or picking an option that is not on the menu. We can also do more in this calculator, such as create an exit option on the menu, and use a while loop so that the menu keeps popping up until we decide to exit. I leave all these things to the viewer to pursue if desired.

Thank you for reading this fun calculator program, it was a joy to make.