Menu Driven Calculator
Code§
#include <print> // For std::print and std::println
#include <scan> // For std::scan
// 1. We create simple, reusable functions for our math operations
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) { return a / b; }
int main()
{
int choice = 0;
double num1 = 0;
double num2 = 0;
std::println("=== Simple Calculator ===");
std::println("1. Add");
std::println("2. Subtract");
std::println("3. Multiply");
std::println("4. Divide");
std::println("5. Exit");
while (true)
{
std::print("\nChoose an option (1-5): ");
auto choiceInput = std::scan<int>(std::stdin, "{}");
choice = choiceInput->value();
if (choice == 5)
{
std::println("Goodbye!");
break; // Breaks out of the while loop to end the program
}
std::print("Enter first number: ");
auto num1Input = std::scan<double>(std::stdin, "{}");
num1 = num1Input->value();
std::print("Enter second number: ");
auto num2Input = std::scan<double>(std::stdin, "{}");
num2 = num2Input->value();
// 2. We use a switch statement to elegantly check the user's choice
switch (choice)
{
case 1:
std::println("Result: {} + {} = {}", num1, num2, add(num1, num2));
break;
case 2:
std::println("Result: {} - {} = {}", num1, num2, subtract(num1, num2));
break;
case 3:
std::println("Result: {} * {} = {}", num1, num2, multiply(num1, num2));
break;
case 4:
// A tiny bit of logic to handle division by zero
if (num2 == 0)
{
std::println("Error: Cannot divide by zero!");
}
else
{
std::println("Result: {} / {} = {}", num1, num2, divide(num1, num2));
}
break;
default:
std::println("Invalid choice! Please choose between 1 and 5.");
break;
}
}
return 0;
}
At this point, you should know how to run the code, and I will not put out the expected output.
Control flow§
Break And Continue§
To break out a loop or a switch statement by directly going to the first line outside the control flow
break
continue
Switch statement§
Switch statement and if statement can behave similarly if intended to, but they are technically different. Let's looks at what switch statement controls
switch (number)
{
case 1:
std::print("a");
case 2:
std::print("b");
case 3:
std::print("c");
}
The code above doesn't have break. Thus,
If number equals 1 then, the final console output is abc, because the code will goes to case 1 and continue on from there.
If number equals 2 then, the final console output is bc, where the code will goes to case 2 and continue from there
If the number equals 3 then, the final console output is c
If we have break statemet in all cases, then it will work exactly like an if statement.
And a default is for cases where non of it matches.
Scan§
auto num1Input = std::scan<double>(std::stdin, "{}");
We have seen that scan can be used to take input from the concole, bu i have not explained the <int> or <double> part of the syntax, <something> is call template, which we will be explaning later. But specifically here, it indicate whether our input is an integer or a double or any type we want for input.
Code style and convention§
If you have noticed so far, cpp actually does not care about spaces or lines we have in the code
if (a == 5)
{
doSomething();
}
////////// OR ////////////
if(a==5){doSomething();}
Work excatly the same and they are the same. Thus, choose you style of your code and stick to it. Some common style is
if ()
{
doSomething();
}
////////////////// OR ////////////////////
if () {
doSomething();
}
///////////////// OR ///////////////////
if () { doSomething(); } // this is only used if the code block inside the if statement is really short.
The same goes to naming convention and style.
Some common convention is doSomething() or do_something() or DoSomething()
Discussion
No comments yet — yours could open the discussion.