I'm making a simple calculator in C++, but I'm having trouble choosing an operator, I wonder who can help me? I'm using this code:
include <iostream>
using namespace std;
int main()
{
string operation = "";
cout << "enter operation:";
if(operation = "/"){
int x;
cin >> x;
int y;
cin >> y;
int sum = x / y;
}
if(operation = "+"){
int x;
cin >> x;
int y;
cin >> y;
int sum = x + y;
}
if(operation = "*"){
int x;
cin >> x;
int y;
cin >> y;
int sum = x * y;
}
if(operation = "-"){
int x;
cin >> x;
int y;
cin >> y;
int sum = x - y;
}
}
I dont know more programming. Who can help me?
I think you're missing a line to read in the operation the user enters. After the line cout << "enter operation:";
, you probably need cin >> operation
.
A couple of other code improvements worth doing:
==
rather than =