#include <cctype>
#include <iostream>
#include <string>
float addition(float number1, float number2)
{
return number1 + number2;
}
float subtraction(float number1, float number2)
{
return number1 - number2;
}
float multiplication(float number1, float number2)
{
return number1 * number2;
}
float division(float number1, float number2)
{
return number1 / number2;
}
bool ask(const std::string& question)
{
std::cout << question;
char answer;
do {
std::cin >> answer;
answer = tolower(answer);
if(answer != 'y' && answer != 'n') std::cout << "invalid input\n";
} while(answer != 'y' && answer != 'n');
return answer == 'y';
}
void menu()
{
float num1;
float num2;
int choice;
std::cout << "\nCalculator function enabled. \n"
"1: addition 2: subtraction 3: multiplication 4: division\n"
"Your selection: ";
std::cin >> choice;
std::cout << '\n';
switch(choice) {
case 1: std::cout << "You are doing an Addition\n"; break;
case 2: std::cout << "You are doing a Subtraction\n"; break;
case 3: std::cout << "You are doing a multiplication\n"; break;
case 4: std::cout << "You are doing a division\n"; break;
default:
std::cout << "Invalid operation\n";
return;
}
std::cout << "First Number: ";
std::cin >> num1;
std::cout << "Second Number: ";
std::cin >> num2;
if(choice == 4 && num2 == 0) {
std::cout << "Inappropriate Operation\n";
return;
}
std::cout << "Your answer is: ";
switch(choice) {
case 1: std::cout << addition(num1, num2) << '\n'; break;
case 2: std::cout << subtraction(num1, num2) << '\n'; break;
case 3: std::cout << multiplication(num1, num2) << '\n'; break;
case 4: std::cout << division(num1, num2) << '\n'; break;
}
}
int main()
{
do {
menu();
} while(ask("\nDo you want to do another operation? Y or N?"));
}