Lindy Lindy - 1 year ago 189
C++ Question

C++: Expression must be a modifiable lvalue

I am making a text based simple operation/trig calculator. I am getting the error: "Expression must be a modifiable lvalue" when I attempt to sqrt the squared variables. Here is my current code: (the error highlights the sqrt)

double a, b, result;

cout << "Please enter your two side lengths (ex:12 5)";
cin >> a, b;
sqrt(pow(a, 2) + pow(b, 2)) = result;

cout << result << endl;

Apologies for any formatting errors, first question asked here any help is appreciated. Thank you!

Answer Source

Modify your cin statement to cin >> a >> b;, this reads multiple values at once, since the comma operator doesn't exist for cin. Also, switch the order of the result variable on the right side of the assignment to left side. The two operands to the = on the assignment are l-values, standing for left-values, supposed to be on the left side, and r-values, standing for right-values, supposed to be on the right side. Switch them, and you get an error.

Your new Modified Code

double a, b, result;

cout << "Please enter your two side lengths (ex:12 5)";

cin >> a >> b; //<-- See '>>' here

//sqrt(pow(a, 2) + pow(b, 2)) = result; <-- switch these to...

result = sqrt(pow(a, 2) + pow(b, 2)); //<-- ...this
cout << result << endl;

Live Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download