0

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?

saviro
  • 11
  • 1
  • 1
  • 5
  • 3
    What do you expect `sum / 12 = cost;` to do? If you are trying to assign to `cost` why would you do it backwards of the two previous assignments? – NathanOliver Oct 05 '16 at 21:59
  • I expect it to take the sum and divide it by 12 and then store that number in cost so that I can return it – saviro Oct 05 '16 at 22:01
  • remember assignment is always assign what is on the right hand side to the left hand side. – NathanOliver Oct 05 '16 at 22:01
  • 1
    @saviro but why then isnt it `m[counting] += sum` but the other way around? – Creris Oct 05 '16 at 22:01
  • 2
    OH I have it backwards, that makes a whole lot of sense now.. Thanks guys! – saviro Oct 05 '16 at 22:03
  • @πάνταῥεῖ Apologies for asking this question, I will not let it happen again. I shouldn't have made such a mistake in the first place and will not make this same mistake again. – saviro Oct 05 '16 at 22:07

1 Answers1

-1

simply LValue must be modifiable. you cannot assign a value to an expression:

sum / 12 = cost; // (sum / 12) is an expression.
Raindrop7
  • 3,889
  • 3
  • 16
  • 27