Building a Simple Calculator CLI
Build a command-line calculator that parses operators and evaluates expressions.
Building a Simple Calculator CLI is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Project Goal
Build a command-line calculator that reads an expression like 3 + 4 and prints the result. We will keep it minimal but well structured.
Required Headers
We need iostream for I/O and string for input.
#include <iostream>
#include <string>
#include <sstream>Main Loop Skeleton
Read a line, parse it, compute, print. Exit on EOF.
int main() {
std::string line;
while (std::getline(std::cin, line)) {
if (line == "quit") break;
double result = evaluate(line);
std::cout << "= " << result << "\n";
}
}Parsing with stringstream
For two operands and one operator, stringstream tokenization is simple and robust.
double evaluate(const std::string& line) {
std::stringstream ss(line);
double a, b;
char op;
ss >> a >> op >> b;
return apply(op, a, b);
}Apply Operator
A switch on the operator character.
double apply(char op, double a, double b) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return b != 0 ? a / b : 0;
default: return 0;
}
}Error Handling
What if the user types something invalid? Check the stream state and report a friendly message.
if (!(ss >> a >> op >> b)) {
std::cout << "Invalid input\n";
return 0;
}Division by Zero
Handle division by zero explicitly. Print an error or throw — whichever fits your design.
if (op == '/' && b == 0) {
throw std::runtime_error("division by zero");
}Adding More Operators
Extend the switch to support more operations: %, ^ for power, or named functions like sqrt.
Maintaining State
Want a running total? Store the last result and let the user start an expression with +.
double last = 0;
// if first token is an operator, use `last` as the first operandPolishing the UX
Print a prompt before each input, accept quit or EOF to exit, ignore blank lines.
Beyond Two Operands
For multi-operand expressions like 3 + 4 * 2, you need an expression parser. The shunting-yard algorithm is the standard approach.
Testing the Calculator
Pipe input from a file to test quickly.
echo "3 + 4\n10 / 2" | ./calcQuick Check
Which standard library facility is convenient for parsing space-separated tokens like 3 + 4?
Recap
A small CLI calculator demonstrates I/O, parsing with stringstream, and switch-driven dispatch. Extend it with error handling, more operators, state, and eventually an expression parser.
Frequently asked questions
Is the “Building a Simple Calculator CLI” lesson free?
Yes — the full text of “Building a Simple Calculator CLI” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Simple Calculator CLI”?
Build a command-line calculator that parses operators and evaluates expressions. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Simple Calculator CLI” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Simple Calculator CLI
- Reading and Writing CSV Files
- A Number Guessing Game
- A Word Frequency Counter