The error message expression result unused
means that you're retrieving or calculating a value that you haven't used, e.g. by storing it in a variable, doing further calculations, or evaluating it in a conditional statement.
When you write:
for(height; height > 0; height--)
{
\\ loop body
}
You're trying to use the variable height
as the control variable for your loop. This is not stylistically appropriate; you should be using a dedicated control variable, which is to say, one that only exists during the scope of the loop and is only used to control the progression of the loop.
You've done this properly in the inner loop, where your control variable is rows
. But in the outer loop, when you reference height
you are retrieving whatever value is stored in that variable and just tossing it at the loop definition, like here! Have an int
! It has no idea what to do with that int
; it wants to initialize a control variable.
If you did use height
this way, you'd be altering its value with the loop update statement. At that point it's no longer storing the value you declared it to store; if I enter 5
for the height of my tower, there's no reason your height
variable should change unless I enter a different number. Just because you've already printed out part of the tower doesn't mean its height has changed.
So to fix this, declare a loop control variable that you initialize with the value of height
. Something like this:
for(int i = height; i > 0; i--)
{
\\ loop body
}
Now you're using the value that you retrieved from height
by assigning it to i
. That's what the compiler is looking for.
You would get this same error from any of these lines:
height;
5;
(494 % 18) - (2 * 1.12);