2017-07-26 51 views
-3

我目前正在做一项工作,我必须使用的结构是c到WAP采取100名员工的输入作为名称,年龄和工资,并显示它作为输出,但我有一些错误,即时通讯无法修复。C代码错误使用结构

PS:新手。

#include <stdio.h> 
#define SIZE 100 


struct employee 
{ 
    int empno; 
    char name[100]; 
    int age, salary; 
} e[100]; 

int main(void) 
{ 
    struct employee emp[100] 
    int i, n; 
    clrscr(); 

     printf("Enter the number of employees\n"); 
     scanf("%d",&n); 
      for (i=0;i<n;i++) 
       { 
        printf("\n Enter employee number : "); 
        scanf("%d",&e[i].empno); 
        printf("\n Enter name of employee : "); 
        scanf("%s",&e[i].name); 
        printf("\n Enter age of employee : "); 
        scanf("%d",&e[i].age); 
        printf("\n Enter salary of employee : "); 
        scanf("%d",&e[i].salary); 
       } 
     printf("\n Emp. No. Name \t Age \t Salary \n\n"); 
      for (i=0;i<n;i++) 
       printf("%d \t %s \t %d \%d \n", 
e[i].empno,e[i].name,e[i].age,e[i].salary); 
return 0; 
} 

,这是错误

prog.c: In function ‘main’: 
prog.c:15:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’ 
     int i, n; 
     ^~~ 
prog.c:16:9: warning: implicit declaration of function ‘clrscr’ [-Wimplicit-function-declaration] 
     clrscr(); 
     ^~~~~~ 
prog.c:25:33: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=] 
         scanf("%s",&e[i].name); 
          ^
+0

护理会比 “一些错误” 更具体的? – interjay

+0

this:prog.c:在函数'main'中: prog.c:15:9:错误:预计'=',',';','asm'或'__attribute__'在'int'之前 int我,n; ^ ~~ prog.c:16:9:warning:隐式声明函数'clrscr'[-Wimplicit-function-declaration] clrscr(); ^ ~~~~~ prog.c:25:33:warning:格式'%s'需要'char *'类型的参数,但参数2的类型为'char(*)[100]'[-Wformat = ] scanf(“%s”,&e [i] .name); ^ – Ron

+2

*有一些错误*是绝对无用的问题描述。我们无法看到您的屏幕或阅读您的想法。 **具体说明**发生了什么*错误*或问题,并提出有关该问题的具体问题**。通过对问题进行编辑并在问题所在的位置添加细节,而不是置身于评论噪音中。 –

回答

4
  1. 删除clrscr功能
  2. 删除e[100]
  3. emp[100]
  4. 修复缩进添加;
  5. 随处可用emp而不是e。使用SIZE并添加MAX_STR_LEN
  6. 变化\%d\t %d
  7. 独立agesalary两行(最佳做法)。
  8. 检查输入n
  9. 在获取员工姓名时还应该添加一些溢出保护。我留给你去研究。

#include <stdio.h> 

#define MAX_STR_LEN 100 
#define SIZE 100 


struct employee 
{ 
    int empno; 
    char name[MAX_STR_LEN]; 
    int age; 
    int salary; 
}; 

int main(void) 
{ 
    struct employee emp[SIZE]; 
    int i, n; 

    printf("Enter the number of employees\n"); 
    scanf("%d",&n); 

    if (n > SIZE) { 
     printf("Too many employees (will process first %d)\n", SIZE); 
     n = SIZE; 
    } 

    for (i = 0; i < n; i++) 
    { 
     printf("\n Enter employee number : "); 
     scanf("%d",&emp[i].empno); 

     printf("\n Enter name of employee : "); 
     scanf("%s",&emp[i].name); 

     printf("\n Enter age of employee : "); 
     scanf("%d",&emp[i].age); 

     printf("\n Enter salary of employee : "); 
     scanf("%d",&emp[i].salary); 
    } 
    printf("\n Emp. No. Name \t Age \t Salary \n\n"); 
    for (i = 0; i < n; i++) 
    { 
     printf("%d \t %s \t %d \t %d \n", 
      emp[i].empno, emp[i].name, emp[i].age, emp[i].salary); 
    } 
    return 0; 
}