#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_EMPLOYEES 100 struct Employee { int id; char name[20]; char gender[5]; int age; int performance; char contact[20]; }; void printEmployee(struct Employee *e) { printf("%d\t %s\t %s\t %d\t %d\t %s\n", e->id, e->name, e->gender, e->age, e->performance, e->contact); } int main() { int n; printf("请输入员工数量:"); scanf("%d", &n); if (n > MAX_EMPLOYEES) { printf("员工数量不能超过 %d\n", MAX_EMPLOYEES); return 1; } struct Employee emp[MAX_EMPLOYEES]; for (int i = 0; i < n; i++) { printf("请输入第 %d 位员工的信息:\n", i+1); printf("\t编号:"); scanf("%d", &(emp[i].id)); printf("\t姓名:"); scanf("%s", emp[i].name); printf("\t性别:"); scanf("%s", emp[i].gender); printf("\t年龄:"); scanf("%d", &(emp[i].age)); printf("\t业绩:"); scanf("%d", &(emp[i].performance)); printf("\t联系方式:"); scanf("%s", emp[i].contact); } int groupNum = n / 5; srand(time(NULL)); printf("本次共分为 %d 个小组,各组员工信息如下:\n", groupNum); for (int i = 0; i < groupNum; i++) { printf("第 %d 个小组:\n", i+1); for (int j = 0; j < 5; j++) { int k = rand() % n; printEmployee(&emp[k]); } } if (n % 5 != 0) { printf("第 %d 个小组:\n", groupNum+1); for (int j = 0; j < n % 5; j++) { int k = rand() % n; printEmployee(&emp[k]); } } return 0; }为什么输入完姓名后就自动退出了
时间: 2024-03-23 12:41:16 浏览: 79
在这段代码中,输入员工姓名时出现自动退出的问题可能是因为 `scanf()` 在读取字符串时会将换行符留在缓存中,导致下一次读取时读取到无效的字符,从而导致程序异常退出。解决这个问题的方法是在读取字符串之前,使用 `scanf("%*c")` 将缓存中的换行符读取并丢弃,或者使用 `fgets()` 函数读取字符串。修改后的代码如下所示:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_EMPLOYEES 100
struct Employee {
int id;
char name[20];
char gender[5];
int age;
int performance;
char contact[20];
};
void printEmployee(struct Employee *e) {
printf("%d\t %s\t %s\t %d\t %d\t %s\n", e->id, e->name, e->gender, e->age, e->performance, e->contact);
}
int main() {
int n;
printf("请输入员工数量:");
scanf("%d", &n);
if (n > MAX_EMPLOYEES) {
printf("员工数量不能超过 %d\n", MAX_EMPLOYEES);
return 1;
}
struct Employee emp[MAX_EMPLOYEES];
for (int i = 0; i < n; i++) {
printf("请输入第 %d 位员工的信息:\n", i+1);
printf("\t编号:");
scanf("%d", &(emp[i].id));
printf("\t姓名:");
scanf("%*c"); // 读取并丢弃换行符
fgets(emp[i].name, 20, stdin); // 使用 fgets() 读取字符串
printf("\t性别:");
scanf("%s", emp[i].gender);
printf("\t年龄:");
scanf("%d", &(emp[i].age));
printf("\t业绩:");
scanf("%d", &(emp[i].performance));
printf("\t联系方式:");
scanf("%s", emp[i].contact);
}
int groupNum = n / 5;
srand(time(NULL));
printf("本次共分为 %d 个小组,各组员工信息如下:\n", groupNum);
for (int i = 0; i < groupNum; i++) {
printf("第 %d 个小组:\n", i+1);
for (int j = 0; j < 5; j++) {
int k = rand() % n;
printEmployee(&emp[k]);
}
}
if (n % 5 != 0) {
printf("第 %d 个小组:\n", groupNum+1);
for (int j = 0; j < n % 5; j++) {
int k = rand() % n;
printEmployee(&emp[k]);
}
}
return 0;
}
```
这样修改后,程序就能正常读取员工姓名了。
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)