Upgrading The Calculator Program Using Binary Logic
#include <iostream>
int main() {
std::cout << "input 2 numbers (seperated by space) to get the sum of them.\n";
float input1, input2;
std::cin >> input1 >> input2;
std::cout << input1 << " + " << input2 << " = " << input1+input2;
}
This is the calculator program we were left with after the variables and inputs chapter.
After prompting the user it takes 2 numbers and outputs the sum of them.
We can add multiple functionalities to our calculator and let the user pick the operation by using if statements.
To do this, we ask the user for an int input that indicates what operation they want to perform.
We use if statements to compare the input to check if we have an operation that matches the value of the input.
Task:
Starting from the code above, add the following functionalities:
- A) Ask the user for what operation they want to perform. Tell them that an input of
1is addition of the 2 numbers, and2is subtraction. Use if statements to determine the output based on what operation type the user opted for. - B) Make use of
if,else if, andelseto create anelsestatement that is reached when a user inputs a operation type that isn't created. If the user inputs9as their operation type, they should be told that the operation doesn't exist. - C) Add support for multiplication and division. For the division, make sure that the user is warned and the calculation isn't made if the second input is 0.
Answer - A
#include <iostream>
int main()
{
std::cout << "input the operation you want to perform\n1=addition\n2=subtraction";
int operation;
std::cin >> operation;
std::cout << "input your 2 numbers (seperated by space).\n";
float input1, input2;
std::cin >> input1 >> input2;
if (operation == 1)
{
std::cout << input1 << " + " << input2 << " = " << input1+input2;
}
else if (operation == 2)
{
std::cout << input1 << " - " << input2 << " = " << input1-input2;
}
}
Here we add an additional prompt to get a value for the variable
operation. We add an aditionalifandif elsestatement that prints one of 2 outputs depending on the operation selected.The
else ifstatement could also be anifstatement. Since we already know thatoperation == 2would befalseifoperation == 1istrueit makes sense to useelse if. This also makes sense for the larger purpose of theif-else ifchain that occurs in part B & C.
Answer - B
#include <iostream>
int main()
{
std::cout << "input the operation you want to perform\n1=addition\n2=subtraction";
int operation;
std::cin >> operation;
std::cout << "input your 2 numbers (seperated by space).\n";
float input1, input2;
std::cin >> input1 >> input2;
if (operation == 1)
{
std::cout << input1 << " + " << input2 << " = " << input1+input2;
}
else if (operation == 2)
{
std::cout << input1 << " - " << input2 << " = " << input1-input2;
}
else
{
std::cout << operation << " isn't a valid operation. \nPlease stick to the specified avaliable operations.\n";
}
}
Here we only add
else. Unlike in taskAtheelse ifcannot be an if statement. We need to make sure that every valid operation is checked before the code reaches theelsestatement. If theelsestatement is reached, we tell the user that their operation was invalid.
Answer - C
#include <iostream>
int main()
{
std::cout << "input the operation you want to perform\n1=addition\n2=subtraction\n3=multiplication\n4=division";
int operation;
std::cin >> operation;
std::cout << "input your 2 numbers (seperated by space).\n";
float input1, input2;
std::cin >> input1 >> input2;
if (operation == 1)
{
std::cout << input1 << " + " << input2 << " = " << input1+input2;
}
else if (operation == 2)
{
std::cout << input1 << " - " << input2 << " = " << input1-input2;
}
else if (operation == 3)
{
std::cout << input1 << " * " << input2 << " = " << input1*input2;
}
else if (operation == 4)
{
// Make sure that our calculation is safe, and let the user know if it isn't.
if (input2 != 0)
{
std::cout << input1 << " / " << input2 << " = " << input1/input2;
}
else
{
std::cout << input1 << " / " << input2 << "Is an unsafe, and invalid calculation.\nYou cannot divide by 0.\n";
}
}
else
{
std::cout << operation << " isn't a valid operation. \nPlease stick to the specified avaliable operations.\n";
}
}
In addition to modifying the starting prompt we add 2
else ifstatements. One per new operation. Before we perform the division caluclation we check thatinput2isn't 0. We tell the user that they can't divide by 0 instead of performing the calculation ifinput2 == 0evaluates astrue.