c - 错误: use of undeclared identifier

标签 c cs50

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

: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(" ");
    }      
}

最佳答案

你最好声明r就在main之后从您当前声明的代码开始 r仅适用于for环形。

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(" ");
    }      
}

关于c - 错误: use of undeclared identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29774795/

相关文章:

c - 在 Mac OS X 10.5 和 Ubuntu 10.4 上如何最好地使用 OpenMP?

c - 在结构中的函数中传递枚举(在 C 中)

C:对无法放入数组的长列表进行排序

c - 如何将一个字符连接成一个字符串?

c - 为什么我的节点都没有被释放? (cs50 pset5 段错误) C

c - 为什么函数参数可以在C中包含地址或变量

c - 从群组的SID获取群组成员的方法是什么?

c - 凯撒密码中最后一个字母后循环回到字母表开头的逻辑

基于C的DES加密密码的破解功能,不起作用?

c - 为什么 while 循环中的条件语句会导致程序选择性地永远暂停?