I wrote this code in C language to create multiple users and login using the username & password provided at the time of user registration in this program. But I am getting results same as my first action.
For example, I have registered using uname as test1 and pass as test123 and if I enter this in the login section then I'll get "Login Successful" message but after when I try to enter any random uname and pass in login section it will show me "Login Successful" msg only. When I exit and re-run the program and if I enter wrong credentials while logging in then I'll get "Invalid Details" msg but after that if I try to log in using the correct credentials then also I get the "Invalid Details" msg. Any idea whats wrong with the code?
Case 1: using correct credentials.
Input 1
Username = test1
Password = test123
Output = Login Successful
Input 2
Username = wrong username
Password = wrong password
Expected output = Invalid Details
but Output I'm getting = Login Successful
Case 2: Using wrong credentials first
Input 1
Username = wrong username
Password = wrong password
Output = Invalid Details
Input 2
Username = test1
Password = test123
Expected Output = Login Successful
but Output I'm getting = Invalid Details
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct database
{
char user[20];
char pass[20];
char email[40];
}record;
int main()
{
int count, choice, entries, i, j, ls, check=0;
char mask;
char fileDump[10000][50];
char username[20];
char password[20];
FILE *fptr = fopen("E:\\login_practice.bin","ab+");
char dump[256];
Again:
printf("Welcome to user authentication program v1.2\n");
printf("\n1. Register\n");
printf("\n2. Login\n");
printf("\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nHow many user you want to register?\n");
printf("\nUsers = ");
scanf("%d", &entries);
for(count=1; count<=entries; count++)
{
FILE *fp = fopen("E:\\email.bin", "ab+");
printf("\nEnter your email: ");
scanf("%s", &record.email);
fprintf(fp, "%s\n", record.email);
fclose(fp);
printf("\nEnter a username: ");
scanf("%s", &record.user);
fprintf(fptr, "%s\n", record.user);
printf("\nEnter a password: ");
for(j=0; j<10; j++)
{
mask = getch();
if(mask == 13)
{
break;
}
else
{
record.pass[j] = mask;
mask = '*';
printf("%c", mask);
}
}
fprintf(fptr, "%s\n", record.pass);
printf("\nRegistration Successful.\n");
}
goto Again;
break;
case 2:
i=0;
while(fgets(dump, sizeof(dump),fptr))
{
strcpy(fileDump[i], dump);
i++;
}
ls=i;
printf("\nLines Scanned = %d\n", ls);
printf("\nEnter your username: ");
scanf("%s", &username);
printf("\nEnter your password: ");
for(j=0; j<10; j++)
{
mask = getch();
if(mask == 13)
{
break;
}
else
{
password[j] = mask;
mask = '*';
printf("%c", mask);
}
}
strcat(username, "\n");
strcat(password, "\n");
for(i=0; i<=ls; i+=2)
{
if(strcmp(fileDump[i], username)==0)
{
if(strcmp(fileDump[i+1], password)==0)
{
check++;
}
}
}
if(check == 1)
{
printf("\nLogin Successful.\n");
}
else
{
printf("\nInvalid Details Entered!\n");
}
goto Again;
break;
case 3:
exit(0);
break;
default:
printf("\nBad Choice!\n");
goto Again;
break;
}
fclose(fptr);
}