user3842085 user3842085 - 3 months ago 17
C Question

C 'for' loop is only looping once

I scoured through the code for the 'for' loop issues here. I checked my semicolons etc,but still cannot figure out why my loop is only looping once.

Code is shown below, Thank you.

#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf("Enter Height: ");
int height = GetInt();
int i;
int count = 1;

for(i=height; i>0; i--)
{
for(i=height-1;i>0;i--)
{
printf(" ");
}
for(i=count;i>0;i--)
{
printf("#");
}
printf(" ");
for(i=count;i>0;i--)
{
printf("#");
}

count++;
printf("\n");
}
}

Answer

You're using the same iterator in the inner loops, while it's being used in the outer(main) one.

Just use an integer called j (declare it earlier of course) and use it in the inner one.