该程序要求员工姓名,年龄和工资。有一个小问题,我不知道这是什么错。当我输入没有空格的名称或只输入名字时,程序正常工作但只要我输入带空格的全名。该程序只是跳过一些值而不询问并跳转到其他值。我知道他们的工作有点不对,工人[i] .name"我也试过%c但没有成功。
struct employee {
int pAge;
float salary;
char name[30];
};
int main(void) {
struct employee worker[2];
for (int i = 0; i < 2; i++) {
printf("Enter Name of %d employee: ", (i+1));
scanf(" %s", worker[i].name);
printf("Enter Age of %d employee: ", (i+1));
scanf("%d", &worker[i].pAge);
printf("Enter Salary of %d employee: ", (i+1));
scanf("%f", &worker[i].salary);
}
printf("\n");
printf("List of All workers\n\n");
printf( "Age\tSalary\t\tName\n");
for (int i = 0; i < 2; i++) {
printf("%d\t%.2f\t\t%s\n", worker[i].pAge, worker[i].salary, worker[i].name);
}
}
答案 0 :(得分:1)
它非常简单,只需使用它:
scanf(" %[^\n]s", worker[i].name);