0

Here is the code

#include stdio.h

#include cs50.h

int main(void)
{
    printf ("Height: \n");

    int height;
    do
    {
        height = get_int();

    }
    while (23 < height || height <= 0);

    for (int row = 1; row <= height; row ++)

        {
                for (int space = height - row; space >= 0; space--)

                {
                    printf(" ");
                }
                 for (int dash= 1; dash <= row +1; dash ++)

                {
                 printf("#");

                }
         printf("\n");
         }
    return 0;

    }

Error messages

:( rejects a height of -1
    timed out while waiting for input to be rejected

:( handles a height of 0 correctly
    did not find EOF

:( handles a height of 1 correctly
    expected "##\n", not " ##\n"

:( handles a height of 2 correctly
    expected " ##\n###\n", not " ##\n ###\n"

:( handles a height of 23 correctly
    expected " ...", not " ..."

:( rejects a height of 24, and then accepts a height of 2
    timed out while waiting for input to be rejected

1 Answer 1

1

You should print within the loop, before the input, so that the checker knows its input got rejected. Fixes first and last error.

You also messed up some comparison operators.

You should repeat input as long as the number is not in the range from 0 to 23. So repeat if it's either less than zero or larger than 23. The 23 < height is correct, while height < 0 should not use a <=. Fixes second error.

The other comparison operator is in for (int space = height - row; space >= 0; space--). For the last line, when height == row, there should be no leading spaces. So you would have to use space > 0 and not >=. Fixes the other three.

1
  • Thank you!.....
    – Kuro
    Commented Nov 6, 2017 at 20:30

You must log in to answer this question.

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