saviro saviro - 2 months ago 17
C++ Question

c++ Expression must be a modifiable lvalue int sum

I am writing a function that has a for loop inside it which adds an array together and then outside of the for loop i want to take the sum of the array and divide it by 12 and place that number in a variable to return to my main cpp file.

Here is my code

int sum = 0;
for (int counting = 0; counting < 15; counting++)
{
sum += m[counting];

}

sum / 12 = cost;



return cost;


The line i'm getting my error on is
sum / 12 = cost;

sum is highlighted red and says "Expression must be a modifiable lvalue" Now I did some searching(not a lot of time atm) on google and here on stackoverflow and cannot find a solution that is similar to mine so I can understand why its wrong and fix it.

Would anyone be able to help me understand what is wrong here so I am able to fix it?

Answer

LValue musn't be modifiable so you cannot assign values to expression:

sum / 12 = cost; // (sum / 12) is an expression.