Skip to content

Robo-Synaptic-Vaish/cpp-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

🧮 C++ Calculator

A menu-driven calculator built in C++ while learning programming fundamentals and gradually improving project structure, object-oriented programming, and code organization.

📌 Project Overview

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)


✨ Features

Version 1

The original calculator supports:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Division-by-zero protection
  • Menu-driven interaction
  • Continuous execution using a while loop

Version 2

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 Calculator class

📂 Project Structure

cpp-calculator/
│
├── include/
│   └── calculator.h
│
├── src/
│   ├── main.cpp
│   └── calculator.cpp
│
├── .gitignore
├── LICENSE
└── README.md

File Responsibilities

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

🧠 Concepts Used

This project demonstrates several C++ concepts learned throughout its development:

  • Variables and data types
  • double for decimal calculations
  • Functions
  • Pass-by-reference
  • switch statements
  • while loops
  • Conditional statements
  • Classes and objects
  • Header files
  • Separate .cpp implementation files
  • Header guards
  • #include
  • Mathematical functions from <cmath>
  • Basic input and error handling
  • Division-by-zero protection

🏗️ Version 2 Architecture

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.


⚙️ How to Compile

From the project root directory, run:

g++ src/main.cpp src/calculator.cpp -o calculator.exe

If compilation succeeds without errors, the executable has been created.


▶️ How to Run

On Windows:

.\calculator.exe

📋 Calculator Menu

The current version provides:

==============================
        CPP CALCULATOR
==============================

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Power
7. Square Root
8. Exit

📚 What I Learned

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 switch statements work well for menu-driven programs.
  • How while loops 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 .h and .cpp files.
  • How classes and objects can organize program functionality.
  • How <cmath> provides mathematical functions such as pow(), sqrt(), and fmod().
  • How to protect a program against invalid mathematical operations.

🚧 Future Improvements

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 History

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.


👩‍💻 Author

Vaishnavi Singh


📄 License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors