简体   繁体   中英

Getting an undeclared identifier error for "i"

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

int main(void)
{
    int height;
    do
    {
        // Asking for # of blocks
        height = get_int("Height: ");
    }

    // Setting min/max height
    while (height < 1 || height > 8);

    // For columns
    for (int i = 0; i < height; i++)

        // For spaces
        for (int j = height - 1; j > i; j--)
        {
            printf(" ");
        }

            // For the #'s
            for (int j = 0; j <= i; j++)
            {
                printf("#");
            }

printf("\n");
}

C is not Python.

This code

// For columns
for (int i = 0; i < height; i++)

    // For spaces
    for (int j = height - 1; j > i; j--)
    {
        printf(" ");
    }

        // For the #'s
        for (int j = 0; j <= i; j++)
        {
            printf("#");
        }

is equivalent to

// For columns
for (int i = 0; i < height; i++)
{
    // For spaces
    for (int j = height - 1; j > i; j--)
    {
        printf(" ");
    }
}

// For the #'s
for (int j = 0; j <= i; j++)
{
    printf("#");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM