Making a calculation based on the operator a user inputs

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;
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';
}

if the operation is declared as a string would this still work?
switch requires an integral type. Even a char array won't work with a switch since it is not an integral type the compiler understands.

A std::string is not a built-in type in C++, so won't work with a switch.

Without a bit of coding voodoo hackery:
https://hbfs.wordpress.com/2017/01/10/strings-in-c-switchcase-statements/

Topic archived. No new replies allowed.