我在编译“未声明的标识符‘i’时遇到错误。我的代码有什么问题?[关闭]

问题描述 投票:0回答:1

有关详细信息,我正在通过 YouTube 上的哈佛 cs50 (2023) 讲座来学习编码。在时间戳 6:34:05,他正在制作一个名为 uppercase 的程序,我也跟着做了。当我编译代码时出现错误,但当他编译时却没有。我错过了什么?

此代码是 C 语言。

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

int main(void)
{
    string s = get_string("Before: ");
    printf("After: ");
    for (int i = 0; i < strlen(s); i++);
    {
        if (s[i] >= 'a' && s[i] <= 'z')
        {
            printf("%c", s[i] - 32);
        }
        else
        {
            printf("%c", s[i]);
        }
    }
    printf("\n");
}

我尝试完全复制他的代码,但仍然不起作用。

c cs50
1个回答
0
投票

一个好的编译器会告诉你这一点:

    test.c:10:40: warning: for loop has empty body [-Wempty-body]
    for (int i = 0; i < strlen(s); i++);
                                       ^
test.c:10:40: note: put the semicolon on a separate line to silence this warning
© www.soinside.com 2019 - 2024. All rights reserved.