0

I don't understand what to put in for the (initialization; condition; update) part of the for loop. I used 4 variables in my code: i = height, r = rows, j = spaces, n = hashes. Whenever I type in "make mario" it comes up with the error of undeclared identifier. Does that mean I have to set all the variables equal to something before I put them in the for loop? Every single time I try to execute my code no hashes or spaces show up. This is what I have so far:

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

int main(void)
{
    do
  {
    printf("Height: ");
    int i = GetInt( );
  }
while (i < 0 || i > 23);

for (int r = 0; r < i; r++);
{
  for (int j = 0; j <= i-1; j--);
  {
      printf(".");
  }
  for (int n = 0; n <= r+2; n++);
  {
      printf("#");
  }
  printf("\n");  
}

}
0

There are several syntax problems here. First, there's a scope issue with the variable i. Any variable that is declared inside of a curly brace pair only exists inside of those curly braces. That means that since i was declared inside of the while loop, it doesn't exist after the loop. Instead, declare it before the loop so that it still exists after the loop.

Next, there are semicolons after every for loop setup. That has the effect of for(....) do nothing; Then, the curly brace pair that follows serves only to define the scope of whatever is inside of the curly braces. It also means that the code block in the curly braces will always execute, but only once. Simple fix: remove the semicolons.

You also have created an infinite loop in your j loop. Since j starts out smaller than i-1, and you are decrementing j on each loop, the loop will never terminate. I'll leave it to you to fix that one.

These fixes will at least get you back on track. You'll need to figure out the logic to correctly produce the pyramid from there. I can't do the whole thing for you, after all!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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