0

I saw this question has been asked in multiple ways before but I believe I've checked answers and am still stumped as to the issue I have.

The output for my pset1 Mario more comfortable appears to be correct, with no extra spaces after the pyramid and the correct number of hashes and rows. However when checking with check50 I receive the following:

:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:) rejects a height of 0
:( handles a height of 1 correctly
    expected ""#  #"", not ""#  #""
    did you add too much trailing whitespace to the end of your pyramid?
:( handles a height of 2 correctly
    expected "" #  #"\n"##  ...", not "" #  #"\n"##  ..."
    did you add too much trailing whitespace to the end of your pyramid?
:( handles a height of 8 correctly
    expected ""       #  #"\...", not ""       #  #"\..."
    did you add too much trailing whitespace to the end of your pyramid?
:( rejects a height of 9, and then accepts a height of 2
    expected "" #  #"\n"##  ...", not "" #  #"\n"##  ..."
    did you add too much trailing whitespace to the end of your pyramid?
:) rejects a non-numeric height of "foo" 
:) rejects a non-numeric height of ""

Here's how my pyramid looks with . instead of spaces:

.....#  #
....##  ##
...###  ###
..####  ####
.#####  #####
######  ######

Here's my code, any help much appreciated I can't see where I may have gone wrong.

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

int main(void)
{
    int height;
    do
    {
        height = get_int("Height: ");
    }
    while (height > 8 || height < 1);
    
// ABOVE User inputs height between 1-8 inclusive
    
// BELOW Pyramid is built using 'for loops' and integers 'row', 'space', 'L' and 'R'. 

    for (int row = 0; row < height; row++)
    {
        int space;
        for (space = (height - 1); space > row; space = space - 1)
        {
            printf(" ");
        }
        int L;
        for (L = 0; L <= row; L++)
        {
            printf("#");
        }
            printf("  ");
        int R;
        for (R = 0; R <= row; R++)
        {
            printf("#");
        }
            printf("\n");
    }
            printf("\n");
}
2

It looks like you're printing an extra new line at the end. You print the last newline at the end of the "row" loop, then another new line. check50 expects exact output, so try removing the last one.

Not an error, but also consider re-writing

int R;
for (R = 0; R <= row; R++)

as

for (int R = 0; R <= row; R++)

With the first way, the R variable exists outside the loop, and it doesn't need to. With the second way, R only exists inside the loop, which is better, especially when the programs you write get longer and longer.

| improve this answer | |
  • This seems to have fixed the problem, thank you! Variables are now also located in my for loops. – t-monny Jun 19 at 17:30

You must log in to answer this question.

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