void basicSettings(struct Student* student, struct Course courses[], int* numCourses) { int choose; printf("\n--------基本设置--------\n"); printf("\n--------1.添加学生信息--------\n"); printf("\n--------2.添加老师信息--------\n"); printf("\n--------3.修改密码--------\n"); printf("\n--------4.返回--------\n"); printf("请输入您的选择:\n"); scanf("%d", &choose); if(choose==1){ printf("\n"); printf("请输入班级:"); scanf("%s", student->className); printf("请输入学号:"); scanf("%s", student->id); printf("请输入姓名:"); scanf("%s", student->name); } if(choose==2) { printf("\n"); printf("请输入课程数:"); scanf("%d", numCourses); printf("请输入每门课程的名称和教师名字:\n"); for (int i = 0; i < *numCourses; i++) { printf("第%d门课程:", i + 1); scanf("%s %s", courses[i].name, courses[i].teacher); } } if(choose==3) { void changePassword(struct User* user) { printf("\n--------修改密码--------\n"); printf("请输入旧密码:"); char oldpassword[MAX_NAME_LEN]; scanf("%s", oldpassword); if (strcmp(oldpassword, user->password) != 0) { printf("密码错误,修改失败!\n"); return; } printf("请输入新密码:"); scanf("%s", user->password); printf("密码已修改!\n"); } changePassword(user_p); } FILE* fp = fopen("student.dat", "wb"); if (fp == NULL) { printf("无法打开文件!\n"); return; } fwrite(student, sizeof(struct Student), 1, fp); fclose(fp); fp = fopen("courses.dat", "wb"); if (fp == NULL) { printf("无法打开文件!\n"); return; } fwrite(courses, sizeof(struct Course), *numCourses, fp); fclose(fp); printf("设置已保存!\n"); } 修改代码
时间: 2023-07-07 07:18:23 浏览: 105
在函数basicSettings中,我发现有一个内嵌的函数changePassword。这个函数的定义在if(choose==3)的条件下,最好将其定义移到函数basicSettings的外面,避免出现嵌套函数的情况。同时,在函数内部,应该检查文件是否成功打开,如果打开失败,应该返回错误信息。修改后的代码如下:
```
void changePassword(struct User* user) {
printf("\n--------修改密码--------\n");
printf("请输入旧密码:");
char oldpassword[MAX_NAME_LEN];
scanf("%s", oldpassword);
if (strcmp(oldpassword, user->password) != 0) {
printf("密码错误,修改失败!\n");
return;
}
printf("请输入新密码:");
scanf("%s", user->password);
printf("密码已修改!\n");
}
void basicSettings(struct Student* student, struct Course courses[], int* numCourses) {
int choose;
printf("\n--------基本设置--------\n");
printf("\n--------1.添加学生信息--------\n");
printf("\n--------2.添加老师信息--------\n");
printf("\n--------3.修改密码--------\n");
printf("\n--------4.返回--------\n");
printf("请输入您的选择:\n");
scanf("%d", &choose);
if (choose == 1) {
printf("\n");
printf("请输入班级:");
scanf("%s", student->className);
printf("请输入学号:");
scanf("%s", student->id);
printf("请输入姓名:");
scanf("%s", student->name);
}
if (choose == 2) {
printf("\n");
printf("请输入课程数:");
scanf("%d", numCourses);
printf("请输入每门课程的名称和教师名字:\n");
for (int i = 0; i < *numCourses; i++) {
printf("第%d门课程:", i + 1);
scanf("%s %s", courses[i].name, courses[i].teacher);
}
}
if (choose == 3) {
changePassword(user_p);
}
FILE* fp = fopen("student.dat", "wb");
if (fp == NULL) {
printf("无法打开学生文件!\n");
return;
}
fwrite(student, sizeof(struct Student), 1, fp);
fclose(fp);
fp = fopen("courses.dat", "wb");
if (fp == NULL) {
printf("无法打开课程文件!\n");
return;
}
fwrite(courses, sizeof(struct Course), *numCourses, fp);
fclose(fp);
printf("设置已保存!\n");
}
```
阅读全文