0

Getting error on cs50 problem set 1 Mario "handles a height of 1 correctly, timed out while waiting for program to exit"

Here's my code:

#include <stdio.h>

#include <cs50.h>

int main(void) { // to get the positive integer int height;

do
{
    height = get_int(" Height: ");
    height -= 1;
}

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

//building block of super mario
//it is for tracking the height and width
for (int i = 0; i <= height; i++)
{

    //it is  for traking spaces
    for (int k = height; k > i; k--)
    {

        printf(" ");
    }
    // it is for tracking hashes
    for (int j = 0; j <= i; j++)
    {

        printf("#");
    }

    printf("\n");
}

}

0
height -= 1;

If the user passes a value of 1 (which is a value the program is supposed to work with), your code decrements it, making it zero. This is then rejected by your bounds test (height <1), so it asks again for input.

Since the program doesn't exit after being given a value of 1, which check50 expects, it gives up waiting after a while.

0

You do not need this line height -= 1; It will cause out of bound errors when checking just as @Sentox as stated.

Rather, modify while (height <1 || height > 7); and adjust your for loop. You have most of the other pieces in place.

You must log in to answer this question.

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