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");
 }

}

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.

| improve this answer | |
0

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

| improve this answer | |
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.

| improve this answer | |

You must log in to answer this question.

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