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
Version: 1.0
This is a menu-driven calculator built using C++. It performs the following arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
The program continues running until the user chooses to exit.
- The entire project is written in C++.
- Functions are used to avoid writing the same code repeatedly.
- Two kinds of repetition are demonstrated in this project:
- Code repetition: Instead of writing the code for taking two numbers inside every
switchcase, a separate function (TakeTwoNumbers) is created and reused. - Program repetition: The calculator keeps running until the user chooses to exit. This is achieved using a while loop.
- Code repetition: Instead of writing the code for taking two numbers inside every
- A switch statement is used to perform different operations based on the user's choice.
- An if statement is used in the division case to prevent division by zero.
- Variables are initialized before use. Initializing variables avoids unpredictable (garbage) values and makes the program behave reliably.
After making changes to the code, save the file (Ctrl + S) in VS Code.
Open the integrated terminal and run:
g++ src/main.cpp -o calculator.exeIf the command executes without any errors, the program has been compiled successfully.
In the terminal, type:
.\calculator.exeThrough this project, I learned:
- How C++ source code is compiled into an executable (
.exe) file. - The difference between source code and executable files.
- How functions help avoid code repetition.
- How passing variables by reference allows a function to modify the original variables.
- How
switchstatements make menu-driven programs easier to write. - How
whileloops allow a program to keep running until a condition becomes false. - Why variables should always be initialized before they are used.
- How to compile and run a C++ program using
g++.
Vaishnavi Singh