Lindy - 2 months ago 10x

C++ Question

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

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.

```
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;
```

Source (Stackoverflow)

Comments