gpt4 book ai didi

c - 使用结构的 C 代码错误

转载 作者:行者123 更新时间:2023-11-30 20:48:10 25 4
gpt4 key购买 nike

我目前正在做一项作业,我必须使用 WAP 的结构 is c 将 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);
^

最佳答案

  1. 删除 clrscr 函数
  2. 删除e[100]
  3. emp[100]之后添加;
  4. 修复缩进
  5. 到处使用 emp 而不是 e
  6. 使用SIZE并添加MAX_STR_LEN
  7. \%d 更改为 \t %d
  8. agesalary 分隔为两行(最佳实践)。
  9. 检查输入n
  10. 在获取员工姓名时还应添加一些溢出保护。我把这个留给你研究。
<小时/>
#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;
}

关于c - 使用结构的 C 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45338636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com