Home > Blockchain >  Program crash in checking userID Parameter in C
Program crash in checking userID Parameter in C

Time:05-20

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

CodePudding user response:

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);
// ...
  • Related