c - 第二次输入密码无法进入
问题描述
这是登录系统,当我第一次输入错误的密码时可以输入,但第二次输入密码时不能接受输入键。它不能接受我放的任何钥匙。第一次键入可以输入,比较有效会跳出循环。现在只有第二次输入不能输入登录成功。
struct registers
{
char id[26];
char password[26];
}user,userLogin;
int login() {
int i = 0, c = 0;
char ch;
FILE*pwd;
struct userLogin;
struct user;
pwd = fopen("password.bin", "rb");
fread(&user, sizeof(struct registers), 1, pwd);
do {
i = 0;//if worng key in first time the userLogin.password[i] will set with zero
printf("Enter user ID:");
scanf("%s", &userLogin.id);
printf("enter user password:");
while (1) {//this is print out the star when user key in password
ch = getch();
if (ch == ENTER || ch == TAB) {
userLogin.password[i++] = '\0';
break;
}
else if (ch == BS) {//when user key in wrong password can delete the password
if (i > 0) {
userLogin.password[i--] = '\0';
//i--;
printf("\b \b");
}
}
else {
userLogin.password[i++] = ch;
printf("* \b");
}
}
while (1) {//compare the user key in password and id and check the file
if (strcmp(user.id, userLogin.id) == 0 && strcmp(user.password, userLogin.password) == 0) {
system("cls");
printf("Login Sucessful!!!\n");
break;
}
else
{
if (!feof(pwd)){//it will read all data stock in the file
fread(&user, sizeof(struct registers), 1, pwd);
}
else {
system("cls");
printf("Invalid password or user id Pls Try Again\n");
fclose(pwd);
c++;
break;
}
}
}
if (strcmp(user.id, userLogin.id) == 0 && strcmp(user.password, userLogin.password) == 0) {
break;//this will check again if login it same will jump out loop
} while (c != 5);
if (c == 5) //if wrong 5 time will close the system
exit(-1);
fclose(pwd);
解决方案
推荐阅读
- bash - 迭代变量路径中的文件(Bash)
- prometheus - 允许 Prometheus alertmanager 接收器字段中的模板化字段
- html - 如何在不改变高度的情况下增加视频的宽度?
- azure - 如何使用 TFS 将 ASP.NET Core 2.0 部署到 Azure
- reactjs - 将 Docker 变量传递给 React index.html 文件
- ios - 使用 Metal 扭曲 iOS 中的视图
- c# - 如何使 for 循环成为通用的?
- gwt - SmartGWT MenuItem 显示检查 canSelect(false)
- sql - 用于将查询结果导出到 Excel 的 MS Acess 按钮
- postgresql - PostgreSQL 和巨大的数组