此程序要求提供员工姓名、年龄和薪水。有一个小问题,我不知道这是什么问题。当我输入不带空格的名字或只输入名字时,程序可以正常工作,但只要我输入带空格的全名。该程序只是跳过一些值而不询问并跳转到其他值。我知道他们缺少“worker[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);
}
}
最佳答案
它非常简单,只需改用它:
scanf(" %[^\n]s", worker[i].name);
关于c - 无法在C程序中输入带空格的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22753070/