0

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

Dean Debrio
  • 57
  • 1
  • 10
  • 1
    A few (possibly unrelated) things: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong); *Always* check what any of the `scanf` family of functions [*returns*](https://en.cppreference.com/w/cpp/io/c/fscanf#Return_value); The C specification explicitly say that passing an input-only stream (like `stdin`) to [`fflush`](https://en.cppreference.com/w/c/io/fflush) leads to *undefined behavior*; And also always remember to check your array indexes, especially in unbounded loops. – Some programmer dude May 19 '22 at 10:13
  • @Someprogrammerdude hello thanks for the comment. Can I have the reference for your second comment? If so is there any way to clean the input since the program also crash when it went into else part of the loop without the fflush() functions. Also, thanks for the thread, I'll check them out. I was taught using feof and scanf so this is pretty new information for me, thanks! – Dean Debrio May 19 '22 at 10:15
  • 1
    C18 **§7.21.5.2** *2* If `stream` points to an output stream or an update stream in which the most recent operation was not input, the `fflush` function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; **otherwise, the behavior is undefined.** – Weather Vane May 19 '22 at 10:19
  • If you will have to clean the input, don't use `scanf`. Input with `fgets` and `sscanf`. Instead of trying to clean the input, just read another string. – Weather Vane May 19 '22 at 10:20
  • @WeatherVane I added another sscanf at the else part, the program crash, like stuck in the "Invalid input, press anything to continue" part. Any idea why that happens? – Dean Debrio May 19 '22 at 10:37
  • Have you changed the `while(!feof())` yet? It's an error which is repeated in bad learning material, and you'll try to process one more item than exists. Use the result of the input conversion to control the loop. With `fgets()` that will be `NULL`, and with `scanf` function family it will be the number of successful conversions (the return value from the function, it's another serious error to overlook). – Weather Vane May 19 '22 at 10:42

1 Answers1

0

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);
// ...
TopchetoEU
  • 697
  • 5
  • 9
  • hey thanks for helping me out, the do while loop is supposed to used for 'walk from point A to B, check if there's any similar account using this same credentials using strcmp(). If you haven't successful then don't stop' Is it wrong to use it for that way? – Dean Debrio May 19 '22 at 10:23
  • Checking for similarity is waay more complicated. Stick to just checking equality – TopchetoEU May 19 '22 at 10:30
  • Okay then should I just put if(idCheck, passCheck == 0) break; over/inside that for loop implying for it to stop checking if it found correct credentials? Because this checking part is actually the simple 'logging in' part – Dean Debrio May 19 '22 at 10:33
  • 1
    Not, break, but `continue`. `continue` will skip the body of the current iteration, but WILL CONTINUE the loop – TopchetoEU May 19 '22 at 10:38