简体   繁体   中英

Why am I not getting proper output in the C program?

I wrote a C program to register a user and login using the same username and password but I am getting Username invalid/doesn't exist message when trying to login. Any idea what I have been doing wrong in the following code? Also when I re-run the program I can't get the Login Successfully message. Even if I use the commented fscanf() function in the code I don't get the proper output.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct database {
    char name[20];
    char email[30];
    char user[10];
    char pass[20];
} store;

int main() {
    int count, entries, choice;
    char username[10];
    char password[20];
    FILE *fptr;
    fptr = fopen("E:\\login.bin", "ab+");
    printf("Welcome to the user authentication program v1.2 .\n");
 Again:
    printf("\n1. Register\n");
    printf("2. Login\n");
    printf("3. Exit\n");
    printf("\nEnter your choice: ");
    scanf("%d", &choice);
    switch(choice) {
      case 1:
        printf("\nEnter the number of users.\n");
        printf("Users = ");
        scanf("%d", &entries);
        for (count = 1; count <= entries; count++) {
            /*printf("\nEnter your email: ");
            scanf("%s", &store.email);
            fprintf(fptr, "%s\n", store.email);*/

            printf("\nEnter a username: ");
            scanf("%s", &store.user);
            fprintf(fptr, "%s\n", store.user);

            printf("\nEnter a password: ");
            scanf("%s", &store.pass);
            fprintf(fptr, "%s\n", store.pass);

            printf("\nRegistration successful.\n");

        }
        goto Again;
        break;

      case 2:
        printf("Enter your username: ");
        scanf("%s", &username);
        ///fscanf(fptr, "%s", &store.user);
        printf("Enter your password: ");
        scanf("%s", &password);
        ///fscanf(fptr, "%s", &store.pass);
        if (strcmp(username, store.user) == 0) {
            if (strcmp(password, store.pass) == 0) {
                printf("\nLogin Successful.\n");
            } else {
                printf("\nIncorrect password!\n");
            }
        } else {
            printf("\nUsername invalid/doesn't exist.\n");
        }
        break;

      case 3:
        exit(1);
        break;
    }
    fclose(fptr);
}

Your program is incomplete. Your writing auth data to a file you never read. You should consider building an array of database records and iterating over each one when you are trying to find a match.

Learn to use your debugger. You would have noticed that only the last auth record created in case 1 is lingering in store when you get to case 2 . You also would have noticed that the fscanf(fptr, "%s", &store.user) was reading only one record.

Consider this program outline:

if exist authfile LoadAuth()
if user enter new auth data, update internal database, then write to file.
if user attempts login, create a temp record with their auth data and then 
iterate through the database, comparing that record to ones in the database.
  If not found, fail the login.

You read the login and password into a local structure and append the credentials to the file, but you never read the file to check against all the login/passwords in the database.

The program only works if you register a single user and log in immediately after. Furthermore you have no error handling, so invalid input can cause undefined behaviour in many places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM