Upgrading The Calculator Program

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:

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 aditional if and if else statement that prints one of 2 outputs depending on the operation selected.

The else if statement could also be an if statement. Since we already know that operation == 2 would be false if operation == 1 is true it makes sense to use else if. This also makes sense for the larger purpose of the if - else if chain 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 task A the else if cannot be an if statement. We need to make sure that every valid operation is checked before the code reaches the else statement. If the else statement 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 if statements. One per new operation. Before we perform the division caluclation we check that input2 isn't 0. We tell the user that they can't divide by 0 instead of performing the calculation if input2 == 0 evaluates as true.

Questions