void Student(struct student stu[N]) { int n = 0,sum=N; FILE* fp = fopen("666.txt", "r"); struct student *p = stu; char num1[13], pass1[10]; char num[13], pass[10]; while (1) { input: printf("请输入学号:"); scanf("%s",&num); printf("请输入密码:"); scanf("%s",&pass); while (fscanf(fp, "%s %s", num1, pass1) != EOF) { if(strcmp(num1, num) == 0 && strcmp(pass1, pass) == 0){ printf("登录成功!\n"); break; } } if (feof(fp)) { printf("你输入错误,请重新输入!\n"); fseek(fp, 0, SEEK_SET); // 文件指针回到文件开头 } else { break; } } fclose(fp); // 关闭文件 } while (n < 4) { printf("亲爱的同学你好,这是你的注册报到流程:\n 注册缴费->宿舍楼领取钥匙->食堂办理饭卡->图书馆办理借书证->注册报到成功\n"); printf("请选择(0-4): "); scanf("%d", &n); switch (n) { case 1:Register(p);break; case 2:LookUpdatePersonal(p);break; case 3:DormitoryCnd(p);break; case 4:system("cls");Print_Map();break; case 0:n = 5;break; default:printf("无效的输入,请重新选择!\n");break; } } } }优化代码
时间: 2024-04-07 19:31:49 浏览: 49
==运算符和Equals()方法区别
void Student(struct student stu[N]) {
int n = 0,sum=N;
FILE* fp = fopen("666.txt", "r");
struct student *p = stu;
char num1[13], pass1[10];
char num[13], pass[10];
int login_success = 0;
while (!login_success) {
printf("请输入学号:");
scanf("%s", num);
printf("请输入密码:");
scanf("%s", pass);
fseek(fp, 0, SEEK_SET);
while (fscanf(fp, "%s %s", num1, pass1) != EOF) {
if(strcmp(num1, num) == 0 && strcmp(pass1, pass) == 0){
printf("登录成功!\n");
login_success = 1;
break;
}
}
if (!login_success) {
printf("你输入错误,请重新输入!\n");
}
}
fclose(fp);
while (n != 5) { // 只有输入 0 才会退出循环
printf("亲爱的同学你好,这是你的注册报到流程:\n 注册缴费->宿舍楼领取钥匙->食堂办理饭卡->图书馆办理借书证->注册报到成功\n");
printf("请选择(0-4): ");
scanf("%d", &n);
switch (n) {
case 1:Register(p);break;
case 2:LookUpdatePersonal(p);break;
case 3:DormitoryCnd(p);break;
case 4:system("cls");Print_Map();break;
case 0:break; // 直接跳出循环
default:printf("无效的输入,请重新选择!\n");break;
}
}
}
阅读全文