0

Here is my code :

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

int main()
{
    int height = -1;
    int line = 0;
    int counter = 0;

    do 
    {
        printf("Enter the height: ");
        scanf("%d", &height);
    }
    while (height < 0 || height > 23);
    printf("\n");

    for(line = 1; line <= height; line++)
    {
        int spaceNum = height - line;
        int spaceCounter = 1;
        for (spaceCounter = 0; spaceCounter < spaceNum ; spaceCounter++)
            printf(" ");
        for(counter = 0; counter <= line ; counter++)
            printf("#");

        printf("\n");
    }


    return 0;
}

I think the program is working except the fact that when I enter 0 it does not ask me for an other number and I can't understand why. On top of that, when I check the program with the CS50 check, it seems as many flaws appear as you can see below:

:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
   \ expected an exit code of 0, not output of "\n"
:( handles a height of 1 correctly
   \ expected output, but not "\n##\n"
:( handles a height of 2 correctly
   \ expected output, but not "\n ##\n###\n"
:( handles a height of 23 correctly
   \ expected output, but not "\n                      ##\n           ..."
:( rejects a height of 24
   \ expected output, but not "\n                      ##\n           ..."
:( rejects a non-numeric height of "foo"
   \ killed by server
:( rejects a non-numeric height of ""
   \ expected output, not a prompt for input
https://sandbox.cs50.net/checks/b7b2820d825d43e0a44c32457d05fcd4

I can't see how to fix it; could anyone help ?

Kind regards,

Antoine Zerbini

3

First, to understand why it doesn't ask for another number when you enter 0, look at your while loop conditions and try to determine what will happen in the while loop and how the code will behave when the value of "height" is 0.

Although, when the program not asking for another number when you enter 0 that is correct!, because when you enter 0 that means you need a column with 0 height, i.e on the floor, so the program should not make any # and exit, same as when you enter a number and the program build the column then exit, so you did a correct thing but you just need to understand how you did it.

Second, your program works fine as an algorithm and as a structure but the reason that the check50 show errors is because the check50 looks for a specific pattern of #s and your program shows the same pattern of #s BUT there is one line difference, please go to Problem Set1 there is an example of how the output of your program should be and compare it to the output of your program, i am sure you will find one different line which make all your #s pattern very different from the check50's point of view.

I hope i helped.

Michael Nageh

You must log in to answer this question.

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