0

Apparently h0 is 'unsed', but im using it in my for loops. What needs to happen for something to be considered 'used'? Why is it like this?

Here is the code and error respectively

CODE:

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

int main(void)
{
    do
    {
        printf("Height: ");
        int h0 = GetInt();
        int h, s, hash;
    }
    while (h0 < 1 ^^ h0 > 23);

    for (h = h0; h > 0; h--)
    {
        for (int i = h - 1; i > 0; i--)
        {
            printf (" ");
        }
        for (int j = h0 - h + 1; j > 0; j--)
        {
            printf("#");
        }

        printf("  ");

        for (int k = h0 - h + 1; k > 0;k--)
        {
            printf("#");
        }
        printf("\n");
    }
}

ERROR:

~/workspace/pset1/ $ make mario
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    mario.c  -lcs50 -lm -o mario
mario.c:11:13: error: unused variable 'h0' [-Werror,-Wunused-variable]
        int h0 = GetInt();
            ^
mario.c:13:12: error: use of undeclared identifier 'h0'
    while (h0 < 1 ^^ h0 > 23);
           ^
mario.c:13:22: error: unknown type name 'h0'
    while (h0 < 1 ^^ h0 > 23);
                     ^
mario.c:13:25: error: expected expression
    while (h0 < 1 ^^ h0 > 23);
                        ^
mario.c:17:14: error: use of undeclared identifier 'h0'
    for (h = h0; h > 0; h--)
             ^
mario.c:23:22: error: use of undeclared identifier 'h0'
        for (int j = h0 - h + 1; j > 0; j--)
                     ^
mario.c:30:22: error: use of undeclared identifier 'h0'
        for (int k = h0 - h + 1; k > 0;k--)
                     ^
mario.c:35:16: error: use of undeclared identifier 'h0'
        return h0;
               ^
8 errors generated.
make: *** [mario] Error 1
0

You have two errors with one root cause. The problem is all a matter of scope. h0 is declared inside of a do/while loop. That means it only exists between the curly braces of the loop. Once outside those curly braces, it no longer exists. That leads to two different error messages.

First, inside the loop, you are declaring h0, but not using it inside the loop.

Second, outside the loop h0 doesn't exist, so you get undeclared variable errors each time you try to use it.

If you declare it before the do/while loop, the problem should go away.

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

You must log in to answer this question.

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