0

I've been working on Mario for a week now. I have found a pattern to create the spaces (which I am replacing with underscores just to see how my code outputs) but they are not printing at all. I also cannot figure out how to print the hashes. I know it has to do with the line you're on vs. the number of lines total as we start out with 2, then 3, then 4 regardless of what the user input for height is. Here's my code:

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

int rows;
int main(void)

{
    do
    {
      printf("Height: ");
      rows = GetInt();
    }
  while (rows > 23 || rows < 1);

  int x;
  int spaces;
  int hashes;

  for (x = 0; x < rows; x++)
    {
        for (spaces = (rows - 1); spaces == 0; spaces--)
        {
            printf("_");
        }

        for (hashes = 0; hashes < 24; hashes++)
        {
            printf("#");
        }
      printf("\n");
    }    
}

My output is this:

Height: 3

# # #

I drew out the pyramid a few times (like the computer would) trying to see a solution. It helped earlier but I am just stuck now. I feel like I am really close to the solution. A hint or a breadcrumb please!

1 Answer 1

0

Why are you printing 24 hashes every row?

Check the boundaries of the loops

EDIT:

You should avoid things like for (...; hashes < 24; ...) {

That 24, which is called a magic number because only you know why it is there, can be replaced by a variable which should vary, and therefore have rows with variable number of hashes, which is what you want.

Never ever ever put a magic number inside a loop.

6
  • Sorry, I didn't see there was code unformatted. I will look for the bug. Please format your code correctly so others can see it. Nov 11, 2016 at 20:30
  • Sorry, having trouble getting it to appear correctly.
    – Lee Gainer
    Nov 11, 2016 at 20:33
  • The output is this: Height: 3 ######################## ######################## ########################
    – Lee Gainer
    Nov 11, 2016 at 20:34
  • OK, now I see it Nov 11, 2016 at 20:35
  • Ya, that's whew I am stuck. I have tried several different combinations of things that I thought would work. The closest I've gotten is an out put of : Height: 3 ## ## ## with each set of hashes on its' own line.
    – Lee Gainer
    Nov 11, 2016 at 21:01

You must log in to answer this question.

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