0

This is diary writing program that gives me problem with login function.

   void MainMeni();
    void PisanjeDnevnika();
    void LogIN(){
    char username[50];
    char password[500];
    FILE * fpointeru = fopen("C:\\Users\\marmun\\Desktop\\dnevnik\\login.txt", "r");
    FILE * fpointerp = fopen("C:\\Users\\marmub\\Desktop\\dnevnik\\login.txt", "r");
    fgets(username, 50, fpointeru);
    fgets(password, 50, fpointerp);
    fgets(password, 50, fpointerp);
    fclose(fpointeru);
    fclose(fpointerp);
    char Username[50];
    char Password[500];
    int i;
    int o;
    do{
    printf("Input user\n");
    scanf("%s", Username);

So if username input is eqqual to username stored in registration before it should login otherwise loop until user matches it. But after user puts the first letter matching the first letter of stored username program breaks and lets user in. Also strcmp only returns 1 and -1.

i = strcmp(username, Username);
        printf("%d", i);
        if(i == 0){
            printf("Login succes!");
            }
        else if(i != 0){
            printf("Login unssuceful\n!");
            LogIN();
            printf("%s", Username);
        }
        system("cls");
        }while(i != 1);
       do{
        printf("Input password\n");
        scanf("%s", Password);
        o = strcmp(password, Password);
        printf("%d\n", o);
        if(o == 1){
            printf("Password correct!\n");  
    }
        else if(o != 1){
            printf("Password inncorect!");
        }

    }while(o != 1);

  char Izbor;

  printf("\Login succesuful!");

  printf("\n\tChoose 1 out of 3 options!");
  printf("\t1.Meni\n\t2.Writing diary.\n\t3.Exit");
  scanf("%c", Izbor);
  
   do
   {

   switch(Izbor){
    case '1':
        MainMeni();
    case '2':
        PisanjeDnevnika();
    case '3':
        break;
}
while(Izbor != '1' || Izbor != '2' || Izbor != '3');
system("cls");
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Luka
  • 1
  • Note that [`scanf("%s", s);` is an unsafe as `gets()`](https://stackoverflow.com/q/26602608/10077), and [one must never use `gets`](https://stackoverflow.com/q/1694036/10077). – Fred Larson Nov 23 '21 at 19:12
  • 1
    I'd recommend using [`fgets`](https://en.cppreference.com/w/c/io/fgets) everywhere you are using `scanf`, but you must take care to [remove the newline character](https://stackoverflow.com/q/2693776/10077). Actually, that's probably the reason for your problem, as you do use `fgets` for reading the username from the file. – Fred Larson Nov 23 '21 at 19:14
  • `scanf("%c", Izbor);` ==> `scanf(" %c", Izbor);` added a space. – Weather Vane Nov 23 '21 at 19:16
  • Why are you opening the login.txt file twice? Why not just open it once and read the username and then the password from it? – Fred Larson Nov 23 '21 at 19:20
  • Luka `fgets(password, 50, fpointerp);` --> `password[]` now has a `'\n'` in it. Is that intended to have `'\n'` in passwords? With `scanf("%s", Password);`, `Password[]` will not contain a `'\n'`. – chux - Reinstate Monica Nov 23 '21 at 19:24
  • You should never use IO functions like `fopen`, `fgets`, `scanf` without checking the return value. You are reading password twice. (If you do this intentionally because you are using the same file with 2 pointers, then you should rework this) What is the content of your file? – Gerhardh Nov 23 '21 at 19:25

1 Answers1

2

For starters there are two calls with the variable password

fgets(password, 50, fpointerp);
fgets(password, 50, fpointerp);

It seems it is a typo.

These calls of fgets

fgets(username, 50, fpointeru);

and

fgets(password, 50, fpointerp);

can append to entered strings the new line character '\n'.

You should remove it as for example

username[ strcspn( username, "\n" ) ] = '\0';
password[ strcspn( password, "\n" ) ] = '\0';

The function strcmp not necessary returns 1 or - 1. It returns a positive number if the first string is greater than the second string or a negative number if the first string is less than the second string.

If two strings are equal each other the function returns 0.

So the conditions in the if statement

    o = strcmp(password, Password);
    printf("%d\n", o);
    if(o == 1){
        printf("Password correct!\n");  
}
    else if(o != 1){
        printf("Password inncorect!");
    }

are incorrect.

Also the conditions in the do-while statements should look like

}while(i != 0);

and

}while(o != 0);

instead of

}while(i != 1);

and

}while(o != 1);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • ...and `strcmp()` certainly doesn't return `1` for "equal". It returns `0`. – Weather Vane Nov 23 '21 at 19:39
  • 1
    Actually, the duplicate password input is not a typo, it's stranger than that. The login.txt file is being opened *twice*. I believe the duplicated input line works, because it has to read the username and then the password. Why there isn't simply a second `fgets` on the same file is beyond me. – Fred Larson Nov 23 '21 at 19:40