1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
using namespace std;
double calculator(double num1, double num2, char operation);
int main() {
double num1, num2, answer;
char operation;
char input;
cout << "Enter first number: " << endl;
cin >> num1;
cout << "Enter second number: " << endl;
cin >> num2;
cout << "Enter operation: (+, -, *, or /) " << endl;
cin >> operation;
cout << "\n" << num1 << " " << operation << " " << num2 << " = " << calculator(num1, num2, operation) << endl;
cout << "Repeat? y for yes, any other key to exit: " << endl;
cin >> input;
while (input == 'y')
{
system("CLS");
cout << "Enter first number: " << endl;
cin >> num1;
cout << "Enter second number: " << endl;
cin >> num2;
cout << "Enter operation: (+, -, *, or /) " << endl;
cin >> operation;
cout << "\n" << num1 << " " << operation << " " << num2 << " = " << calculator(num1, num2, operation) << endl;
cout << "Repeat? y for yes, any other key to exit: " << endl;
cin >> input;
}
system("pause");
}
double calculator(const double num1, const double num2, const char operation)
{
if (operation == '+')
{
return num1 + num2;
}
else if (operation == '-')
{
return num1 - num2;
}
else if (operation == '*')
{
return num1 * num2;
}
else if ((operation == '/') && (num1 != 0))
{
return num1 / num2;
}
else
cout << "Invalid. ";
}
|