C++ Mini Project — Calculator
In this page:
Structuring Operations
Splitting each math operation -- addition, subtraction, multiplication, division -- into its own small function keeps the calculator's logic organized and testable individually, rather than tangled together inside one large block of conditional branches.
Example: Structuring Operations
// Include std::cout and std::cin
#include <iostream>
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << add(3, 2) << " " << subtract(3, 2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Designing the Menu
A text-based menu prints the available operations and reads the user's choice, translating that input into which function gets called next -- this is the control-flow backbone that ties the individual operation functions together into a usable program.
Example: Designing the Menu
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare choice and set it to 1
int choice = 1;
// Print to the console with cout
std::cout << "1. Add\n2. Subtract" << std::endl;
if (choice == 1) std::cout << "Add selected" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Guard Against Division by Zero
Dividing by zero is undefined behavior for integers (and produces infinity or NaN for floating-point), and can crash or silently corrupt a program's output.
Checking that the divisor isn't zero before performing division, and reporting a clear error instead, is a mandatory safeguard in any calculator.
Example: Guard Against Division by Zero
// Include std::cout and std::cin
#include <iostream>
double safeDivide(double a, double b) {
// Check whether b == 0
if (b == 0) {
// Print to the console with cout
std::cout << "Cannot divide by zero" << std::endl;
// Send 0 back to the caller
return 0;
}
// Send a / b back to the caller
return a / b;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << safeDivide(10, 0) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Storing Calculation History
Storing each calculation's inputs and result in a std::vector as they happen lets you build a history feature -- printing every previous calculation back to the user on request, without needing to persist anything to disk.
Example: Storing Calculation History
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named history (a resizable array)
std::vector<double> history;
history.push_back(3 + 2);
history.push_back(10 - 4);
for (double result : history) std::cout << result << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Continuous Calculation Flow
Feeding a calculation's result back in as the input to the next operation (rather than starting fresh each time) lets users chain a sequence of operations together, the same way a physical calculator's running total works.
Example: Continuous Calculation Flow
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to 5
double result = 5;
result = result + 3;
result = result * 2;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dividing by zero without checking the divisor first, which crashes for ints or gives infinity for doubles.
- Not validating the menu input, so a letter typed instead of a number breaks the input loop.
- Using
intfor the numbers so results like5 / 2lose the decimal part.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced