Eby
Eby

Reputation: 93

how to do loop in c programming

I want to make login code, i use struct for the user and password(still learning) if the user name or password is wrong how to make the function reload again... im confused..

struct user_info {
    int id_num;
    int password;
};

struct user_info user[16];

int load_user_info() {
user[0].id_num = 10; user[0].password = 1004;
user[1].id_num = 20; user[1].password = 2004;

return 0;}

check login code:

int check_login(int id, int passwd) {

    int idd = id;
    int iddd;
    int passs;
    int i;
    int j;


printf("id_num : ");
scanf("%d", &id);
printf("password: ");
scanf("%d", &passwd);


    for (i = 0; i < 17; i++)
    {
        if (idd == user[i].id_num)
        {
            iddd = i;
            break;
        }
        else if ( i == 16 )
        {
            printf("해당 아이디가 없습니다. 다시 로그인 하세요\n");
            return -1;
        }
    }

    for (j = 0; j < 17; j++)
    {
        passs = 0;
        if (passwd == user[j].password)
        {
            passs = j;
            printf("%d\n", j);
            return id;

        }
        else if (j == 16)
        {
            printf("파스워드가 다릅니다. 다지 로그인 하세요\n");
            return -1;
        }
    }
}

Upvotes: 0

Views: 60

Answers (1)

Anibal
Anibal

Reputation: 11

Just return 0 if it works properly, otherwise return an error code (for example -1 as you did).

Then:

int return_value = check_login(id, pass);

while (return_value) {
    //here you can change id and pass values
    return_value = check_login(id, pass);
}

Upvotes: 1

Related Questions