Help with cs50 mario

:) mario.c compiles
:) rejects a height of -1
:) handles a height of 0 correctly
:( handles a height of 1 correctly
   \ expected output, but not "      ##\n     ##\n    ##\n   ##\n  ##\..."
:( handles a height of 2 correctly
   \ expected output, but not "      ###\n     ###\n    ###\n   ###\n ..."
:( handles a height of 23 correctly
   \ expected output, but not "      ########################\n     ##..."
:( rejects a height of 24, and then accepts a height of 2
   \ expected output, but not "      ###\n     ###\n    ###\n   ###\n ..."
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""```
       
       // declare Height
       int height;
      
     do {
         printf ("Height:");
         height = get_int();
          
          
         
            //Handle 0
          if(height == 0) {
             return  0;
         }
     }
       
        while ( height > 23 || height < 1 );

        
         for (int rows = 0;  rows < 5; rows++)
    {               
         for (int spaces = 0 + (rows - 1); spaces < 5 ; spaces++)
         {
                            printf("%s", " ");
                            
                                                                    }
        for (int hashs = 0; hashs <  height + 1  ; hashs++) // loop for hashs
        {
            printf("#");
        }                                                           
     printf("\n");
         
    }

  
}

You should probably post this at cs50’s reddit, people there are more likely to help you.
Also, the errors show you what the problem is, your for loops are wrong. Take a look at them and understand what they do.

1 Like

you are almost there
why did you write 5 here:
for (int rows = 0; rows < **5**; rows++)

1 Like

I’m assuming you’re doing the “less comfortable” version of the problem, based on your code.

First off, you’re looping from 0 to 5 for the rows, do you think you need to loop from 0 to something else instead? Second, printf("%s", " "); can simply be written as printf(" ");, no need for the string formatting.

Finally, just remember that for each row, you need to print height - (rows + 1) spaces, and rows + 1 hash symbols.

For example, assuming the user’s input is 4 for height, then the pyramid would look like this (had to use periods for the spaces since spaces didn’t show for some reason):

...# – 4 - (0 + 1) spaces, 0 + 1 hash symbols
..## – 4 - (1 + 1) spaces, 1 + 1 hash symbols
.### – 4 - (2 + 1) spaces, 2 + 1 hash symbols
#### – 4 - (3 + 1) spaces, 3 + 1 hash symbols

I hope this hint helps you solve the problem. :wink: Let me know if you manage to get it working properly or if you need more help.

1 Like

I got it!!! thanks so much!