0

I am looking at this code I made for the hashes and spaces, and in my head it seems correct. However, it obviously is not since when I run it, it just keeps going and going. I guess it is called an infinite loop. Can anyone nudge me in the right direction?

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < height; j--)
        {
        printf("%s", " ");
        }
    for (int k = 0; k < height+2; k++)
        {
        printf("#");
        }
 printf("\n");
 }

}

0

3 Answers 3

2

Your j loop uses j--. If j starts at 0 and then keeps decreasing by 1, it will never meet the stop condition of j=height.

0

Try to put into words what your second loop (the one iterating over j) is doing.

0

You only need to say

printf(" ");

not

printf("%s", " ");

Also in the

for (int j = 0; j< height; j--)

Int J will always be less than the height because 1 is subtracted from J. It will go on forever.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .