This is my first C++ project, built while learning the fundamentals of C++. The goal of this project was not only to build a calculator but also to understand variables, functions, switch statements, loops, and references through practical implementation.
A menu-driven calculator built in C++ while learning programming fundamentals and gradually improving project structure, object-oriented programming, and code organization.
This project started as a simple beginner-friendly C++ calculator and has gradually evolved into a more structured application.
The project currently contains Version 2, which keeps the original calculator functionality while introducing a dedicated Calculator class and additional mathematical operations.
Current Version: 2.0 (Work in Progress)
The original calculator supports:
- Addition
- Subtraction
- Multiplication
- Division
- Division-by-zero protection
- Menu-driven interaction
- Continuous execution using a
whileloop
The calculator has been enhanced with:
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Power
- Square Root
- Decimal number support
- Division-by-zero protection
- Modulus-by-zero protection
- Negative square-root validation
- Separate header and implementation files
- Object-oriented
Calculatorclass
cpp-calculator/
│
├── include/
│ └── calculator.h
│
├── src/
│ ├── main.cpp
│ └── calculator.cpp
│
├── .gitignore
├── LICENSE
└── README.md
include/calculator.h
Contains the declaration of the Calculator class and its mathematical operations.
src/calculator.cpp
Contains the implementation of the calculator operations.
src/main.cpp
Handles:
- Displaying the menu
- Taking user input
- Calling calculator functions
- Handling user-facing errors
- Controlling the program loop
This project demonstrates several C++ concepts learned throughout its development:
- Variables and data types
doublefor decimal calculations- Functions
- Pass-by-reference
switchstatementswhileloops- Conditional statements
- Classes and objects
- Header files
- Separate
.cppimplementation files - Header guards
#include- Mathematical functions from
<cmath> - Basic input and error handling
- Division-by-zero protection
Version 1 performed calculations directly inside main.cpp.
Version 2 separates the responsibilities:
main.cpp
│
│ creates
▼
Calculator object
│
┌───────────┼───────────┐
▼ ▼ ▼
add() subtract() multiply()
│
├── divide()
├── modulus()
├── power()
└── squareRoot()
This makes the project easier to understand, maintain, and extend.
From the project root directory, run:
g++ src/main.cpp src/calculator.cpp -o calculator.exeIf compilation succeeds without errors, the executable has been created.
On Windows:
.\calculator.exeThe current version provides:
==============================
CPP CALCULATOR
==============================
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. Square Root
8. Exit
Through the development of this project, I learned:
- How C++ programs are compiled using
g++. - How functions help reduce repeated code.
- How references allow functions to modify original variables.
- How
switchstatements work well for menu-driven programs. - How
whileloops keep a program running until an exit condition is reached. - Why variables should be initialized before use.
- How to work with decimal values using
double. - How to separate declarations and implementations using
.hand.cppfiles. - How classes and objects can organize program functionality.
- How
<cmath>provides mathematical functions such aspow(),sqrt(), andfmod(). - How to protect a program against invalid mathematical operations.
Version 2 is still being developed.
Planned improvements include:
- Input validation
- Better handling of invalid user input
- Calculation history
- Cleaner user interface
- Improved error handling
- More mathematical operations
- Testing
- Further code refactoring
| Version | Description |
|---|---|
| v1.0 | Basic menu-driven calculator |
| v2.0 | Structured calculator with Calculator class and advanced operations |
| v2.x | Future improvements and refinements |
The original version remains preserved in Git history while new versions are developed on separate branches.
Vaishnavi Singh
This project is licensed under the MIT License.