A console calculator in C++. Prompts for an operator and two numbers, prints the result to two decimal places, and loops until you decline another calculation.
Learning-exercise code from 2023 — small, single-file, no dependencies.
- Four operations:
+,-,*,/ - Fixed two-decimal output (
std::setprecision(2)) - Guards division by zero instead of producing
inf - Rejects an unknown operator with
Invalid operator!and re-prompts - Repeats while you answer
y/Y
The source file is named cpp with no extension — pass it to the compiler
explicitly, or rename it to main.cpp first.
g++ -x c++ -o basic-calc cpp
./basic-calcEnter operator (+, -, *, /): *
Enter two numbers: 7 6
7.00 * 6.00 = 42.00
Do you want to perform another calculation? (y/n): n
Input is read with std::cin >> and is not validated — typing a non-number puts
the stream into a fail state and the loop spins. Add cin.clear() +
cin.ignore() handling if you want it robust.