I have a problem with registering a new employee for my project.
It can only detect the first employeeID as an existing..
Here's my code:
void registerNewEmployee(){
FILE *fp;
struct employee obj;
fp=fopen("data.txt","a+");
system("cls");
printf("\n\t*******************************************");
printf("\n\t*******************************************");
printf("\n\t***\t\t\t\t\t***");
printf("\n\t***\tNEW EMPLOYEE REGISTRATION\t***");
printf("\n\t***\t\t\t\t\t***");
printf("\n\t*******************************************");
printf("\n\t*******************************************\n\n");
char employeeIDNew[5];
printf("Employee Number: ");
scanf("%s", &employeeIDNew);
while(fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW) == 4){
if(strcmpi(obj.employeeID,employeeIDNew)==0){
printf("Already Exits!");
}
else{
fscanf(fp,"%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
printf("First Name: ");
scanf(" %s",obj.employeeFN);
printf("Last Name: ");
scanf("%s",&obj.employeeLN);
printf("Password: ");
scanf("%s",&obj.employeePW);
strcpy(obj.employeeID,employeeIDNew);
fprintf(fp,"\n%s\n%s %s\n%s\n", obj.employeeID, obj.employeeFN, obj.employeeLN, obj.employeePW);
break;
}
}
fclose(fp);
}
Example output that I am getting:
Employee Number: 0001
Already Exist!
-----------------------------
Employee Number: 0002
First Name: test
Last Name: employee
Password: testpw
----------------------------
The 0002 entry is not suppose to ask for employee details.
Also even it did ask for the details, it did not add in data.txt
If anybody could help me with this, it would be greatly appreciated.