我目前正在进行一项我必须使用结构的作业是指向WAP输入100名员工作为姓名,年龄和薪水并将其显示为输出但是我有一些错误,我不是能够解决。
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 :(得分:4)
clrscr
功能e[100]
;
emp[100]
emp
代替e
。SIZE
并添加MAX_STR_LEN
。\%d
更改为\t %d
age
和salary
分隔为两行(最佳做法)。n
。#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;
}