1

I have approached this problem by using a forloop with two nested loops. The second nested loop works and prints the correct number of hashes. The first nested loops does not appear to working at all, and I cannot figure out why. It always seems to immediately break from the loop.

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

int get_height();

int main(void)
{
    int height = get_height();
    for (int i=0; i<height; i++)
    {
        int j=i;
        do
        {
            printf(" ");
            j+=1;
        }
        while (height-j>1);

        int k=0;
        do
        {
            printf("#");
            k+=1;
        }
        while (k-i<2);
    printf("\n");
    }
}

int get_height(void)
{
    int n;
    do
    {
        printf("Height: ");
        n = get_int();
    }
    while (n<0 || n>23);
    return n;
}
1

For the first inner loop, use a while loop, not do..while, as you don't want any space printed in front on the last line.

The difference between while and do..while is that the do..while runs at least once.

Here, I'd prefer for loops instead.

1
  • Ahh right, thank you so much! May 4 '17 at 15:54

You must log in to answer this question.

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