I am new to C++ (Coming from python). When I am comparing a string in an If statement it is always false.
#include <iostream>
#include <string>
using namespace std;
int main(){
string op;
int num1;
int num2;
cout << "Operator? ('+', '-', '*', '/' )" << endl;
cin >> op;
cout << "Number 1?" << endl;
cin >> num1;
cout << "Number 2?" << endl;
cin >> num2;
int result;
cout << op << endl;
if (op == "+"){
int result = num1 + num2;
}
if (op == "-"){
int result = num1 - num2;
}
if (op == "*"){
int result = num1 * num2;
}
if (op == "/"){
int result = num1 / num2;
}
else{
cout << "Invalid Operator" << endl;
return 0;
}
cout << "Result is " << result << endl;
}
When I feed it the inputs '+', '1', and '1', It prints '+', meaning it read the input. But then I expect it to give me 'The result is 2', but it prints "Invalid operator". What can i do to fix this?