error: use of undeclared identifier

error: use of undeclared identifier

我正在尝试 运行 此代码,但我不断收到:

:21:20: error: use of undeclared identifier 'r'
for(int s = h - r; s > 0;s--)

但我似乎无法弄清楚为什么会出现此错误。有人可以帮我吗?

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

int main(void)
{
    int h;
    do
    {
        printf("please give me a positive int between 1 and 23: ");
        h = GetInt();
    }
    while (h < 1 || h > 23);
    //get int and store it 


    for(int r = 0; r < h; r++)
    {
        printf("#");
    }

    for(int s = h - r; s > 0;s--)
    {
        printf(" ");
    }      
}

您最好在 main 开始后立即声明 r,因为在您当前的代码中,您仅为 for 循环声明 r

int main() {
    int r=0, h=0;

    // add other code here
    // don't forget h=GetInt()

    for(r = 0; r < h; r++)
    {
        printf("#");
    }

    for(int s = h - r; s > 0;s--)
    {
        printf(" ");
    }      
}