这是登录系统,当我第一次输入错误的密码可以输入但第二次输入的密码不能接受输入键时。它不能接受我放的任何钥匙。第一次输入可以输入并比较有效的键将跳出循环。现在只有第二次输入的密钥无法输入回车才能成功登录。
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);