Home > Back-end >  Why is my variable (h) considered undeclared?
Why is my variable (h) considered undeclared?

Time:11-17

I wrote a longer version of this code and had no problems but now I keep getting this error (error: use of undeclared identifier 'h') on my my 25th line (while (9 < h);). Please tell me what I'm doing wrong

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

int main(void)
{
    do
    {
        int h = get_int("Height:");
        if (h < 9)
        {
            printf("%i\n", h);
            {
                for (int j = 0; j < h; j  )
                {
                    for (int k = j   1; k < h; k  )
                    {
                        printf("#");
                    }
                    printf("\n");
                }
            }
        }
    } while (9 < h);

    {
        if (h > 0)
    }
}

I've tried switching the bracket placement but this is the only one that seems valid. Basically I'm expecting the the Height to be reask if the number enter isn't between 1-8. Any guesses why this isn't working? Also here's the longer version of my code that I'm attempting to shorten.

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

int main(void)
{
    // get height between 1 - 8
    int h = get_int("Height:");
    if (h < 9)
    {
        if (h > 0)
        {
            printf("%i\n", h);
            {
                // Rows
                for (int j = 0; j < h; j  )
                {
                    // Columns
                    for (int k = j - 1; k < h; k  )
                    {
                        printf("#");
                    }
                    printf("\n");
                }
            }
        }
    }
    else
    {
        int i;
        {
            // Loop if # outside 1-8 entered
            do
            {
                i = get_int("Valid Height:");
                {
                    printf("%i\n", i);
                    {
                        for (int l = 0; l < i; l  )
                        {
                            for(int m = 0; m < i; m  )
                            {
                                printf("#");
                            }
                            printf("\n");
                        }
                    }
                }

            } while (9 < i);

            {
                if (i > 0)
                {}
            }
        }
    }
}

CodePudding user response:

The variable h is undeclared because the variable is defined in the scope of the do-while, inside the curly braces, but the condition of the do-while loop isn't in the same scope. To fix this, at the beginning of main() add the line int h; and then remove the word int from the line int h = get_int("Height:");. Now your h variable will be declared. Also, an if with no body is a syntax error, so I added some brackets. With these changes, your first block of code will look like this:

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

int main(void)
{
    int h;
    do
    {
        h = get_int("Height:");
        if (h < 9)
        {
            printf("%i\n", h);
            {
                for (int j = 0; j < h; j  )
                {
                    for (int k = j   1; k < h; k  )
                    {
                        printf("#");
                    }
                    printf("\n");
                }
            }
        }
    } while (9 < h);

    {
        if (h > 0)
        {
           /* some code */
        }
    }
}
  • Related