0

I am banging my head against my desk with this. I have written the code and it executes without any errors. However when i input the number to advise how high i want Mario to climb, the mouse cursor flickers and moves down the screen at a rapid pace and no pyramid appears. Can anyone advise what could be the problem?? I have a feeling it is something to do with one of the loops but my brain is fried on this on.

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

int main (void)

{
    int height;
    int rows;
    int spaces;
    int hashes;

    do
    {
        printf("Enter the height of the pyramid you want Mario to jump\n");
        height = get_int();
    }
    while(height < 0 || height > 23);

    for (rows = 0; rows <= height; rows++)
    {
        for (spaces = 0; (spaces = (height - rows)); spaces--)
        {
            printf(" ");
        }
        for (hashes = 1; (hashes <= (rows + 2)); hashes++)
        {
            printf("#");
        }
        printf("\n");
    }
return (0);
}
1

I'm no expert (only on week 2 myself) but in addition to the numbers problem that Blauelf mentioned you also seem to have misused the < and = signs in your cipher. For example you want rows to keep counting up ++ while rows are less than height, not less than or equal to because this would give you one more row than height.

0

Your numbers are not exactly right (for example, your outer loop would run height+1 times), but the infinite loop is

for (spaces = 0; (spaces = (height - rows)); spaces--)

So let's disect this. Initially, spaces is set to 0. Fine. But the condition is an assignment, assigning height-rows to spaces. This loop will continue as long as the assigned value, height-rows, is not 0. After each iteration, spaces is decremented, but the assignment in the condition replaces the value instantly.

That works because in C everything that's not 0 (0, false, '\0', NULL are some examples of 0) is considered true (not necessarily if you compare it to literal value true, which is 1, but if used in conditions).

Change this for loop to more closely resemble the other two, adjust the loop bounds a bit, and you should be ok.

BTW, return is not a function, so return (0); is same as return 0;.

You must log in to answer this question.

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