To add to what Patrice has said, "=" is an assignment operator: The value on the right hand side is "put into" the variable on the left. "==" is a comparison operator which returns either 0 or 1 depending on the two values.
If they re the same, it returns 0. If not, it returns 1.
But C++ is based on the much older language C where 0 means "false" and any non-zero value means "true" - and an assignment operator returns the value that it set, so you can "chain" them:
a = b = c = 666;
assigns the value 666 to
c
, then returns 666 which is then assigned to
b
, which returns 666 to be assigned to
a
.
So when you write
if (z = 1)
the value of
z
is overwritten with 1 and 1 is returned to be used as the comparison - and since it's non-zero it is always true.
And the same thing happens with the next comparison!
I'd also suggest that you look at either
if (z == 1)
...
else if (z == 2)
...
else if (z == 3)
...
else if (z == 4)
...
else
... tell him you don't understand ...
or better, use a
switch
block.