Expression must be a modifiable lvalue

i got a problem at line 15, thank you for your kind heart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream> 
using namespace std;

int main (){
    cout << "Simple Calculator: \n";
    cout << "";
    cout << "Enter a Operator [+ , - , / , *]\n";
    char op;
    cin >> op;

    if(op == '+'){
        cout << "Enter two digits:\n";
        double x,y,ans;
        cin >> x >> y;
        x + y = ans;

    }
}
I assume that you meant
ans = x + y;
not
x + y = ans;

Note that ans is not going to live outside the limited scope in which it is defined; so if you want to print the answer outside of that then you will have to define ans outside of it too.
Note that = is not an "equality operator" like in math.

It's an assignment operator. The expression on the right is evaluated and the resulting value is assigned to the variable on the left.

After this statement there is no relation between x, y and ans. Modifying one of the variables afterwards will not automatically affect the others.
Last edited on
Thank you everyone that solved it
Topic archived. No new replies allowed.