0

Any suggestions on how to fix this error? "mario.c:10:14: error: declaration shadows a local variable" Code sample below. Thanks in advance!

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

int get_positive_int(string prompt);

int main(void)
{
    int h = get_positive_int("What height do you want: ");
//print the pyramid
for (int h = 0; h < n; h++)
  {
      for (int j = 0; j < n; j++)
      {
          printf("#");
      }
      printf("\n");
  }
// Prompt user for positive integer
int get_positive_int(string prompt)
{
    int n;
    do
    {
        n = get_int("%s", prompt);
    }
    while (n < 1);
    return n;
}

2

Look at this code:

    int h = get_positive_int("What height do you want: ");
//print the pyramid
for (int h = 0; h < n; h++)

It declares h (as an int) twice. Change one of them.

Also, n hasn't been declared before being used. Maybe you meant the first one to be n and not h?

There are other issues that you'll discover, but this answers the specific question about your shadow var.

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 .