C++ || Class – A Simple Calculator Implementation Using A Class, Enum List, Typedef & Header Files

Print Friendly, PDF & Email

The following is another homework assignment which was presented in a programming class, that was used to introduce the concept of the class data structure, which is very similar to the struct data structure.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Header Files - How To Use Them
Class - Data Structure
Enumerated List
Typedef
Do/While Loop
Passing a Value By Reference
Constant Variables
Atoi - Convert String To Int Value

This is an interactive program which simulates a basic arithmetic calculator, where the user has the option of selecting from 9 modes of operation. Of those modes, the user has the option of adding, subtracting, multiplying, and dividing two numbers together. Also included in the calculator is a mode which deducts percentages from any given variable the user desires, so for example, if the user wanted to deduct 23% from the number 87, the program would display the reduced value of 66.99.

A sample of the menu is as followed:
(Where the user would enter numbers 1-9 to select a choice)

This program was implemented into 3 different files (two .cpp files, and one header file .h). So the code for this program will be broken up into 3 sections, the main file (.cpp), the header file (.h), and the implementation of the functions within the header file (.cpp).

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.

======== File #1 Main.cpp ========


======== File #2 CCalc.h. ========

Remember, you need to name the header file the same as the #include from the main.cpp file. This file contains the function declarations, but no implementation of those functions takes place here.

======== File #3 CCalc.cpp. ========

This is the function implementation file for the CCalc.h class. This file can be named anything you wish as long as you #include “CCalc.h”


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 0
------------------------------------------------------------

You have entered an invalid command...
Please ENTER to try again.
------------------------------------------------------------

Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 2
------------------------------------------------------------

Please enter a new value to assign: 2

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 3
------------------------------------------------------------

The current value is: 2

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 4
------------------------------------------------------------

The current value is: 2

Please enter a number to be added to 2: 2

2 + 2 = 4

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 5
------------------------------------------------------------

The current value is: 4

Please enter a number to be subtracted from 4: 6

4 - 6 = -2

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 6
------------------------------------------------------------

The current value is: -2

Please enter a divisor to be divided by -2: -2

-2 ÷ -2 = 1

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 7
------------------------------------------------------------

The current value is: 1

Please enter a number to be multiplied by 1: 87

1 x 87 = 87

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 8
------------------------------------------------------------

The current value is: 87

Please enter a percentage to be deducted from 87: 23

23% off of 87 is 66.99

------------------------------------------------------------
Calculator Options:
1) Clear Calculator
2) Set Initial Calculator Value
3) Display The Current Value
4) Add
5) Subtract
6) Divide
7) Multiply
8.) Percentage Calculator
9) Quit

Please enter a selection: 9
------------------------------------------------------------

Calculator will now quit
------------------------------------------------------------

Bye....

Was this article helpful?
👍 YesNo

Leave a Reply