So here is my problem:
#include "stdafx.h"
int kvad(int tal) {
int sum;
tal * tal = sum; /* The left "tal" has the error: Expression must be a modifiable lvalue*/
return sum;
}
int kub(int alt) {
int pro;
alt * alt * alt = pro; /* The left "alt" has the error: Expression must be a modifiable lvalue*/
return pro;
}
int _tmain(int argc, _TCHAR* argv[])
{
int ggr, gda, tre, tva;
printf("Hur många tal att multiplicera: ");
scanf_s("%d", ggr);
printf("\n i i * i i * i * i\n=== ======= ===========\n");
for (gda = 1; gda <= ggr; gda++) {
tva = kvad(gda);
tre = kub(gda);
printf("%2d%6d%10d\n", gda, tva, tre);
}
return 0;
}
I do not know if the last part is needed but I am not sure so I did include it anyway.
I know that there are other threads with similar problems but I can't find the solution there.
An lvalue
is a value that can be assigned to, and if often a variable. By contrast, an rvalue
is a value that cannot be assigned to. The names come from the trend that lvalue
s tend to appear on the left side of an assignment, whereas rvalue
s tend to appear on the right side of an assignment. In your code, you have:
tal * tal = sum;
Which is an error because tal * tal
yields a value that can't be assigned to. The expression results in a number, but this is different from a variable because it doesn't make sense to assign a number to another number. It would be like saying 5 = variable
.
Also remember that assignment is non commutative. That is, tal * tal = sum
is not equivalent to sum = tal * tal
. The name on the left of the =
is what will be assigned the value on the right, so the name on the left must always be something that can be assigned to, that is, an lvalue
.