Putting it together
- C++ course
- Beginner
- Lesson 23/116
- 6 min
Combine all parts into a working calculator.
Putting it together
In the final lesson we will combine all parts of the calculator: the four operation methods, the zero check, and a loop over a char array to run all operations in sequence.
We put the four operators into an array with char ops[] = {'+', '-', '*', '/'};. Then a for loop iterates and a switch selects the correct method for each operator.
This structure is the basis of real calculator apps: the user enters an operator, the program picks the right operation and shows the result. Here we simulate the input with a fixed array.
for and switch together
A for loop has three parts: init (int i = 0), condition (i < 4) and step (i++). While the condition is true, the body runs.
switch compares a value against case labels. If ops[i] is '+', the case '+' branch runs. Each branch ends with break; to exit the switch.
Example: switch (ops[i]) { case '+': result = calc.add(a, b); break; ... }. This structure selects the right method by operator. The loop repeats for each operator.
Practice Assemble the calculator
Inside main, the ops array is ready. Below the comment, add a for loop: for (int i = 0; i < 4; i++). In the loop body, first declare double result = 0;, then add switch (ops[i]) with four case branches: '+' → calc.add(a, b), '-' → calc.subtract(a, b), '*' → calc.multiply(a, b), '/' → calc.divide(a, b). Each branch assigns to result and ends with break;. After the switch, print the result with std::cout << result << std::endl;. For a=20, b=5 the output must be 25, 15, 100, 4.
Starter code
#include <iostream>
class Calculator {
public:
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) {
if (b == 0) return 0;
return a / b;
}
};
int main() {
Calculator calc;
double a = 20;
double b = 5;
char ops[] = {'+', '-', '*', '/'};
// Loop over ops with for, select operation with switch and print the result
return 0;
}Sample solution
#include <iostream>
class Calculator {
public:
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) {
if (b == 0) return 0;
return a / b;
}
};
int main() {
Calculator calc;
double a = 20;
double b = 5;
char ops[] = {'+', '-', '*', '/'};
// Loop over ops with for, select operation with switch and print the result
for (int i = 0; i < 4; i++) {
double result = 0;
switch (ops[i]) {
case '+': result = calc.add(a, b); break;
case '-': result = calc.subtract(a, b); break;
case '*': result = calc.multiply(a, b); break;
case '/': result = calc.divide(a, b); break;
}
std::cout << result << std::endl;
}
return 0;
}Expected output
25 15 100 4
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the switch statement do?
- It creates a loop
- It creates a class
- It declares a variable
- It selects a branch by value Correct answer
Explanation: switch runs different branches based on a value.
2 for loop
____ (int i = 0; i < 4; i++) { }
for (int i = 0; i < 4; i++) { }
Explanation: The for loop iterates a counter step by step.
3 Match loop and switch concepts
| Term | Meaning |
|---|---|
for |
Counted loop |
switch |
Branch by value |
case '+': |
Addition branch |
break; |
Exit switch |