← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 15 of 17

C++ Mini Project — Calculator

This mini project builds a calculator that asks for numbers and an operation and shows the answer. It pulls together what you have learned, like building a small model after learning each part.

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Dividing by zero without checking the divisor first, which crashes for ints or gives infinity for doubles.
  2. Not validating the menu input, so a letter typed instead of a number breaks the input loop.
  3. Using int for the numbers so results like 5 / 2 lose the decimal part.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.