0

Okay, I can't figure out why the below doesn't work properly - all it does is print out new lines (as many as pyramid_height's value), no hashes and (I don't think) any spaces.

This is my code:

#include <stdio.h>
#include <cs50.h>

int main(void)
{

int pyramid_height = 0;

do
    {
    printf("Please enter the half pyramid's height\n");
    pyramid_height = GetInt();
    } while (pyramid_height < 0 || pyramid_height > 23);

//Setting up 3 variables as int.
//i should effectively be the line/row number
//hash should be the line/row number + 2, e.g. the first row (row 0) has 2 hashes    printed.
//spaces should be the pyramid_height - hash, e.g. the first row (row 0) has 6 spaces on it if pyramid_height == 8, as 8-2 = 6     

for (int i = 0, hash = i + 2, spaces = pyramid_height - hash; i < pyramid_height; i++)
    {    

    for (int j = 0; j == spaces; j++)
        {
         printf (" ");
        }

    for (int k = 0; k == hash; k++)
        {
        printf ("#");
        }

    printf("\n");
    }

}

Obviously my two nested loops are just being skipped, so only the \n is getting printed.

My guess is that I'm trying to declare the variables 'hash' and 'spaces' in the wrong place, but i'm not really sure :|

Am I at least correct so far as that you can use the for loop's 'i' counter as the row number, and then work from there?

1 Answer 1

1

The nested loops are being skipped because you write

for (int j = 0; j == spaces; j++)

when you actually meant to write this(most probably)

for (int j = 0; j <= spaces; j++)

The same with the next loop. Imo you should figure out the reason on yourself, why so. Leave a comment if you still find it difficult to figure out the reason. Best of Luck.

0

You must log in to answer this question.

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