Enter an operator (+, -, ×, /): +
Enter two operands: 5
8
5.0 + 8.0 = 13.0
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "Enter an operator (+, -, *, /): ";
char operation;
std::cin >> operation;
if(!std::cin)
{
std::cout << "Bad input\n";
return 1;
}
std::cout << "Enter two operands: ";
double operand1, operand2;
std::cin >> operand1 >> operand2;
if(!std::cin)
{
std::cout << "Bad input\n";
return 1;
}
double result;
switch (operation)
{
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
default: std::cout << "Unexpected operator\n"; return 1;
}
std::cout << std::fixed<< std::setprecision(1)
<< operand1 << " " << operation << " "
<< operand2 << " = " << result << "\n";
return 0;
}
Comments
Leave a comment