-2

I have a school project that requires me to create a C program that includes password protection. I have a function handling logins but when a wrong password is entered the function still allows the user to continue through to the main section of the program. I tried using abort and exit but they don't help. It seems like they just exit the function instead of the program.

Here is my code.

    void userlogin(void){
FILE *fp;
char uName[10], pwd[10];int i;char c;

pUser=(struct user *)malloc(sizeof(struct user));

printf("1. Login Through An Existing Account\n2. Create New account\n");
scanf("%d",& i);
//system("cls");
switch(i){
    case 1:
        if ( ( fp=fopen("user.dat", "r+")) == NULL) {
            if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                printf ("Could not open file\n");
                exit ( 1);
            }
        }
        printf("Username: ");
        scanf("%9s",uName);
        printf("Password: ");
        scanf("%9s",pwd);
        while ( fread (pUser, sizeof(struct user), 1, fp) == 1) {
            if( strcmp ( pUser->username, uName) == 0) {
                printf ("Match username\n");
                if( strcmp ( pUser->password, pwd) == 0) {
                    printf ("Match password\n");
                    //accessUser();
                }
                else if ( strcmp ( pUser->username, uName) == 1){
                    printf("INCORRECT PASSWORD\n");
                    printf("ACCESS DENIED");
                    abort();
                }
            }
        }
        break;

    case 2:
        do
        {
            if ( ( fp=fopen("user.dat", "a+")) == NULL) {
                if ( ( fp=fopen("user.dat", "w+")) == NULL) {
                    printf ("Could not open file\n");
                    exit ( 1);
                }
            }
            printf("Choose A Username: ");
            scanf("%9s",pUser->username);
            printf("Choose A Password: ");
            scanf("%9s",pUser->password);
            fwrite (pUser, sizeof(struct user), 1, fp);
            printf("Add another account? (Y/N): ");
            scanf(" %c",&c);//skip leading whitespace
        }while(c=='Y'||c=='y');
        break;
}
printf("Welcome %s\n", uName);
free ( pUser);//free allocated memory
fclose(fp);

}

Enver Browne
  • 23
  • 1
  • 4
  • 6
    `exit` will exit the program, not just the function. Are you sure you are hitting that code path? – mnistic Apr 06 '18 at 02:35
  • 1
    This might be a good time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Apr 06 '18 at 02:36
  • 2
    Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If you want our help then we need an MCVE, together with the actual input and the expected and actual output. And know the contents of your file as well. – Some programmer dude Apr 06 '18 at 02:38
  • 2
    I suspect code should be `strcmp ( pUser->username, uName) == 1` --> `strcmp (...) != 0` – chux - Reinstate Monica Apr 06 '18 at 03:11
  • 1
    [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Apr 06 '18 at 03:34
  • 1
    Yes there is a way - you can call abort or exit... – user253751 Apr 06 '18 at 04:54
  • http://idownvotedbecau.se/beingunresponsive, http://idownvotedbecau.se/nodebugging/, http://idownvotedbecau.se/itsnotworking/ and http://idownvotedbecau.se/nomcve/ – Some programmer dude Apr 06 '18 at 14:49

1 Answers1

0

I think there are likely other errors in your code

exit() causes normal program termination to occur.

abort() causes abnormal program termination.

If your program is not exiting it is because neither of those functions are being executed.