if(z=1)
"=" is an
assignment operator; "==" is a
comparison operator.
And because C (on which C++ is based) did not have a boolean datatype, any non-zero value is taken as "true".
So your code:
if(z=1)
Sets
z
to 1, decides that 1 is true and always executes the code block.
I'd also strongly recommend that you either use
if ... else if ... else
instead of
if ... if ... if ...
- or better look at a
switch
statement instead.
But ... thirty seconds with a debugger would have shown you what was happening for yourself, and saved you some time. It's well worth investing some effort in learning how to use the debugger while you are working on simple little programs like this: trying to debug a 100K line monster for the first time would be a horrible way to get started!