0

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!

Lindy
  • 11
  • 2

4 Answers4

3

First, cin >> a, b does not do what you think it's doing. You'd better write cin >> a >> b.

Then, you assign data to variables, not variables to data. So do result = sqrt(...).

Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98
2

You've switched around your assignment. You're trying to assign the sqrt function the value of result; you want to assign the result to the value of the sqrt output:

double a, b, result;

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

cout << result << endl;
Cameron637
  • 1,699
  • 13
  • 21
1

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

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
1

The main problem is the line number 4 and 5 of your piece of code.

cin >> a,b; is wrong. You can take multiple input in a line like this cin >> a >> b;

And in the line number 5 you are doing assignment in wrong order. You should just reverse it result this way result = sqrt(pow(a, 2) + pow(b, 2)) ;

Shravan40
  • 8,922
  • 6
  • 28
  • 48