I have attached data.txt file with contains all of the employees data.
The problem is when I login, it would only read one record.
Here's my code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int login();
void displayWelcome();
main(){
int result;
do{
system("cls");
result = login();
if(result==1 || result==2){
system("cls");
if(result==1){
displayWelcome();
}
else{
printf("Welcome Admin!");
}
}
else {
printf("Wrong ID/Password. Please try again\n");
getch();
}
}while(result!=1 && result!=2);
system("pause");
}
struct employee{
char employeeID [5];
char employeeFN [20],employeeLN [20], employeePW [20];
};
int login(){
FILE *fp;
struct employee obj;
fp=fopen("data.txt","r");
printf("EmployeeID:");
char loginEmployeeID[5];
scanf("%s", &loginEmployeeID);
printf("Password:");
char loginEmployeePW[20];
scanf("%s", &loginEmployeePW);
while(!feof(fp)){
fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
if(strcmpi(obj.employeeID,loginEmployeeID)==0 && strcmpi(obj.employeePW,loginEmployeePW)==0){
return 1;
}
else if(strcmpi("admin",loginEmployeeID)==0 && strcmpi("admin",loginEmployeePW)==0){
return 2;
}
else {
return 0;
}
}
fclose(fp);
}
void displayWelcome(){
FILE *fp;
struct employee obj;
fp=fopen("data.txt","r");
fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
printf("Welcome %s %s!\n", obj.employeeFN, obj.employeeLN);
fclose(fp);
}
This code would produce the ff:
Employee ID:admin
Password:admin
Welcome Admin!
--------------------------------------------
Employee ID:0001
Password:password
Welcome Test Employee!
Employee ID: 0002
Password: 2employ
Wrong ID/Password. Please try again (this should not be wrong.)
If anybody could help me with this, it would be greatly appreciated.