0

I'm getting some errors when I run my code through check50 such as

handles a height of 0 correctly,

handles a height of 1 correctly,

handles a height of 2 correctly,

handles a height of 23 correctly,

rejects a height of 24 correctly,

I don't understand the first 4 error messages and I don't think the last one is correct since the spec says

However, to make things more interesting, first prompt the user for the half-pyramid’s height, a non-negative integer no greater than 23

I thought this meant 23 was highest height I could make and 24 should be rejected.

Here is my code:

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

void PrintSpaces(int x);
void PrintHashes(int x);

int main(void)
{
    int n = 0;
    // prompts the user for the height of the pyramid //
    do
    {
        printf("Give me a number between 1 and 23. This will be the height for the pyramid : ");
        n = GetInt();
    }
    while (n < 1 || n > 23);
    printf("Height = %d\n", n); 
    //Prints a number of spaces and hashes such that they add up to n on each line and form a half pyramid, with the most hashes on the bottom.
    for(int i = 0; i < n; i++)
    {
        PrintSpaces(n - i - 1);
        PrintHashes(i + 1);
    }   
}

void PrintSpaces(int x)
{
    for (int b = 0; b < x; b++)
    {
        printf("%s", " ");
    }
}

void PrintHashes(int x)
{
    for (int b = 0; b < x; b++)
    {
        printf("%s", "#");
    }
    printf("\n");
}   
    enter preformatted text here
1
  • The last check makes sure you don't accept 24 as valid. Commented Jul 26, 2018 at 16:54

2 Answers 2

1

Play around with the staff implementation of Mario. Your program should permit pyramid heights of 0 and 1 respectively. Currently, your code prompts the user for valid input for heights of 1 or 0 as if they were invalid.

1
> do
>     {
>         printf("Give me a number between 1 and 23. This will be the height for the pyramid : ");
>         n = GetInt();
>     }
>     while (n < 1 || n > 23);

Hey, I posted a question 4 days ago about the same problem. If the user chooses 0 you don't ask them for a new number, you simply accept 0. That's how mario by cs50 staff is and that's how check50 says you handle 0 height correctly. You have to change one little thing in this part of the code.

You must log in to answer this question.

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