首页 > 解决方案 > 第二次输入密码无法进入

问题描述

这是登录系统,当我第一次输入错误的密码时可以输入,但第二次输入密码时不能接受输入键。它不能接受我放的任何钥匙。第一次键入可以输入,比较有效会跳出循环。现在只有第二次输入不能输入登录成功。


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);

标签: c

解决方案


推荐阅读