Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program crash in checking userID Parameter in C

Hello let’s be quick to the problem. So there’s this login program I wrote:

   void login(){
    int i, j, a=0, idCheck, passCheck;
    char userid[20], password[10], ch, rd;
    USERDATA userArr[20];


    //preparing .txt file to read
    char *userData = "user.txt";
    FILE *fp = fopen(userData, "r");

    if(fp == NULL){
        printf("\n\n\n\t\t\t\t\tError: Couldn't open file %s\n\n\n\n\n\n\n", userData);
        printf("\n\n\n\t\t\t\t\tPress enter to continue\n\t\t\t\t\t");

        return 1;
    }

    while(!feof(fp)){
        USERDATA newUser;
        fscanf(fp, "%[^#]#%[^#]#\n", newUser.username, newUser.password);
        userArr[a] = newUser;                                                   
        a++;
    }
    fclose(fp);

    printf("\n\n\n\t\t\t\t\t=============    login    =============\n\n\n\n\n\n\n");
    printf("\t\t\t\t\t username/email   :    ");scanf("%s", &userid);
    printf("\t\t\t\t\t password         :    ");scanf("%s", &password);

    for(j=0; j < a; j++){

        idCheck = strcmp(userArr[j].username, userid);
        passCheck = strcmp(userArr[j].password, password);

        if(idCheck == 0 && passCheck == 0){
            printf("\n\n\n\t\t\t\t\t Login is successfully done    ");
        } else {

            printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
            printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");


        }

    }
}

In this section

for(j=0; j < a; j++){
        
        do{
            idCheck = strcmp(userArr[j].username, userid);
            passCheck = strcmp(userArr[j].password, password);


        }while(passCheck != 0 && idCheck != 0);

        if(idCheck == 0 && passCheck == 0){
            printf("\n\n\n\t\t\t\t\t  Login is successfully done");
            loginMenu(userid, password);
        } else if(idCheck, passCheck == 1 || idCheck, passCheck == -1){

            printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
            printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");
            getch();
            fflush(stdin);
            system("cls");

        } else {
            printf("here");
            getch();
            fflush(stdin);
            system("cls");
        }

I want to do a little do-while loop on the idCheck and passcheck, but when I did so the program ended up freezing or possibly crashing. Is there any better way to do this? Which part I did wrong? I’ve tried moving in and out the if conditions but it remains the same

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t understand why you’re using a do-while loop. The only thing you need to do is just record the comparison values in the two variables. strcmp compares the whole string, not just the first character.

Your broken section should look something like this after the fix:

for(j=0; j < a; j++){
    idCheck = strcmp(userArr[j].username, userid);
    passCheck = strcmp(userArr[j].password, password);
// ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading