How do I allow the user to input an operator?

Can someone tell me how I can let the user input a certain operator into this calculator function please?

I would like to use cin to input the operator if possible because it is one of the only input commands I know. I'm sorry for being such a noob, I just started c++ 3 days ago.

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
// Calculator.cpp : Defines the entry point for the console application.
// Calculates the value of two numbers based on the four main operations.

#include "stdafx.h"

#include <iostream>


int add	(int x, int y);

int subtract(int x, int y);

int multiply(int x, int y);

int divide(int x, int y);


int main()
{
	using namespace std;
	int x;
	int y;
	cout << "Calculator v1"<< endl;
	cout << "Please Enter A Number"<< endl;
	cin  >> x;
	cout << "Please Enter A Second Number"<< endl;
	cin	 >> y;
	cout << "What Operation Do You Wish To Perform?"<< endl;
	cout << "Choose From +, -, *, And /"<< endl;
	return 0;
}


int add(int x, int y)
{
	return x + y;
}


int subtract(int x, int y)
{
	return x - y;
}


int multiply(int x, int y)
{
	return x * y;
}


int divide(int x, int y)
{
	return x / y;
}


Thanks a ton, Avra.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
cout << "What Operation Do You Wish To Perform?"<< endl;
cout << "Choose From +, -, *, And /"<< endl;

char operation = '+' ;
cin >> operation ;

int result = 0 ;
switch(operation)
{ 
    case '+': result = add(x,y) ; break ;
    // etc. 
Last edited on
Topic archived. No new replies allowed.