Search code examples
clesscs50

CS50 mario-less problem: Pyramid is printing but still have "did you add too much trailing whitespace to the end of your pyramid"?


I'm really new at coding so as expected I run into problems. I cannot understand why my "very simple" solution for Mario Less problem doesn't pass the run tests. I know that it doesn't follow the exact steps as mentioned, but the output of the pyramid is correct (as I think). I have no problem with learning new stuff, so I'm not trying to cheat out my way thru CS50. I just want to know why this option is not correct. The pyramid is printed, but I have "not pass":

:( handles a height of 1 correctly Cause expected ""#"", not ""#"" did you add too much trailing whitespace to the end of your pyramid? comment on check50.

Also checks for different inputs are not passed.

Thanks.

my code:

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

int main(void)
{
    int height;

    // user communication
    do
    {
        height = get_int("Height of the pyramid in steps: ");
        if (height < 1 || height > 8)
        {
        printf("Height must be between 1 an 8 steps. Try again. \n");
        }
    }

    while (height < 1 || height > 8);

    //build pyramid

        if (height == 1)
        printf("#");
        if (height == 2)
        printf(" #\n##");
        if (height == 3)
        printf("  #\n ##\n###");
        if (height == 4)
        printf("   #\n  ##\n ###\n####");
        if (height == 5)
        printf("    #\n   ##\n  ###\n ####\n#####");
        if (height == 6)
        printf("     #\n    ##\n   ###\n  ####\n #####\n######");
        if (height == 7)
        printf("      #\n     ##\n    ###\n   ####\n  #####\n ######\n#######");
        if (height == 8)
        printf("       #\n      ##\n     ###\n    ####\n   #####\n  ######\n #######\n########");

}

Solution

  • The problem with your code is due to syntax mistakes:

    Incorrect:

    if (height == 2)
    printf(" #\n##"); // something missing here
    

    Correct:

    if (height == 2)
    {
        printf(" #\n##\n");
    }
    

    Besides your approach, it's better to use a for loop in this exercises for it helps you learn C using its potential.