Making a calculation based on the operator a user inputs
Nov 23, 2020 at 4:32pm
Hello,
not sure how to go about solving this.
1 2 3 4 5 6
|
cout << "Enter the first number: ";
cin >> numberOne;
cout << "Enter the second number: ";
cin >> numberTwo;
cout << "Enter an operator (+.-.*,/): ";
cin >> operation;
|
Nov 23, 2020 at 5:06pm
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
|
#include <iostream>
using namespace std;
int main()
{
int numberOne {}, numberTwo {};
char operation {};
cout << "Enter the first number: ";
cin >> numberOne;
cout << "Enter the second number: ";
cin >> numberTwo;
cout << "Enter an operator (+.-.*,/): ";
cin >> operation;
switch (operation) {
case '+': cout << numberOne + numberTwo; break;
case '-': cout << numberOne - numberTwo; break;
case '*': cout << numberOne * numberTwo; break;
case '/': cout << numberOne / numberTwo; break;
default: cout << "unknown operand";
}
cout << '\n';
}
|
Nov 23, 2020 at 9:17pm
if the operation is declared as a string would this still work?
Nov 23, 2020 at 9:29pm
Topic archived. No new replies allowed.