繁体   English   中英

错误C2143:语法错误:缺少&#39;;&#39; 在之前 <class-head> &#39; 指导

[英]Error C2143: syntax error : missing ';' before '<class-head>' in struct

当我编译这段代码时:

#include <stdio.h>
void main() {
int n, i, total=0;
printf("Enter the number of employees");
scanf("%d", &n);

struct emprecord
{
    int salary, total;
    char name[50];
};
struct emprecord emp[50];
for (i=0; i<n; i++) {
    printf("Enter the name of employee %d", i+1);
    scanf("%s", &emp[i].name);
    printf("Enter the salary of employee %d", i+1);
    scanf("%d", &emp[i].salary);
    total=total+emp[i].salary;
}
printf("Total salary is: %d", total);
}

我收到以下错误,我假设所有错误都将在第一个错误解决后得到修复:

C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(8):错误C2143:
语法错误:缺少';' 之前''
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(10):错误C2143:
语法错误:缺少';' 在“类型”之前
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(12):错误C2133:'emp':未知大小
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2059:语法错误:'for'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2143:
语法错误:'<'之前缺少'{'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2059:语法错误:'<'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2143:
语法错误:“ ++”前缺少“ {”
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2059:语法错误:'++'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(13):错误C2059:语法错误:')'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(20):错误C2143:
语法错误:“字符串”前缺少“)”
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(20):错误C2143:
语法错误:“字符串”前缺少“ {”
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(20):错误C2059:语法错误:''
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(20):错误C2059:语法错误:')'
C:\\ Users \\ shihab130489 \\ Documents \\ Cpp18888r6u54ru.c(21):错误C2059:语法错误:'}'

有人可以帮助您解决第一个错误吗? 我无法理解问题所在。

Microsoft C编译器(似乎在VS2013之前)仅接受C89 / C90,并且仅允许在函数中的任何可执行语句之前进行类型和变量定义。 您正在尝试在一些可执行语句之后声明结构。 在C ++,C99和C11中有效,但在C90中无效。

因此:

#include <stdio.h>
int main(void)
{
    int n, i, total = 0;
    struct emprecord
    {
        int salary, total;
        char name[50];
    };
    struct emprecord emp[50];
    printf("Enter the number of employees");
    if (scanf("%d", &n) != 1)
    {
        fprintf(stderr, "Did not read a number successfully\n");
        return 1;
    }
    if (n <= 0 || n > 50)
    {
        fprintf(stderr, "Error: you entered %d but it should be in the range 1..50\n", n);
        return 1;
    }

    for (i = 0; i < n; i++)
    {
        printf("Enter the name of employee %d", i+1);
        if (scanf("%s", &emp[i].name) != 1)
            break;  // Sloppy but effective
        printf("Enter the salary of employee %d", i+1);
        if (scanf("%d", &emp[i].salary) != 1)
            break;  // Sloppy but effective
        total += emp[i].salary;
    }
    printf("Total salary is: %d\n", total);
    return 0;
}

当您只对薪水感兴趣时,使人们输入姓名很残酷。

最好将结构定义放在main之前。

但是,当我使用gcc 4.5.1时没有错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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