0

I am getting following errors with check50. Need help to understand which part of my code cant handle these situations.

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

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

:( handles a height of 23 correctly expected "

:( rejects a height of 24, and then accepts a height of 2 expected " ##\n###\n", not "##\n"

int main(void)
{
    int height;
    int hashes;
    int rows;
    int spaces;

    // do while loop which prompts user for input 0 to 23
    do
    {
        height = get_int("Enter a number between 0 to 23: ");
    }
    while (height < 0 || height > 23);

    // Print hashes based on height
    // Print row first
    for (rows = 1; rows < height; rows++ )
    {
        // Print spaces
        for (spaces = height - rows; spaces > 1; spaces--)
        {
            printf(" ");
        }

        // Print hashes and moves to next row
        for (hashes = 0; hashes <= rows; hashes++)
        {
            printf("#");
        }
        printf("\n");
    }
}

Solved: here is my final code which seems to be correct according to check50.

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

int main(void)
{
int height;
int rows;
do
{
    height = get_int("Enter a number between 0 to 23: ");
}
while (height < 0 || height > 23);
rows = height;

for (;0 < height && 24 > height; height--)
{
    for (int space = 0; space < height -1; space++)
    {
        printf(" ");
    }
    for (int hash = 0; hash < rows - (height - 2); hash++)
    {
        printf("#");
    }
    printf("\n");
}
}
1

Let's have a look at one of those error messages:

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

This means that for a height of 2, the test expected to get:

 ##
###

while the actual output of your program was:

 ##

So your program should print two output lines when height is 2.

But if I look into your program loop, I see:

for (rows = 1; rows < height; rows++ )
{
    ...
}

...


And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • i have tried to put rows = 0, hashes = 1.. but cant get to that level. if i put rows = 0; it starts printing like this # ## – Gallilio Oct 12 '18 at 12:36
  • 1
    hashes = 1 instead of = 0 would mean one hash less per row, but you need one more. For rows being 0, you want two hashes (and height - 1 spaces in front, which looks about right to me) – Blauelf Oct 12 '18 at 15:38

You must log in to answer this question.

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