Browse Source

refactoring: changed some variable names for readability and used pointers to conserve memory in the functions CheckEmployeeCredentials() and EmployeeCredentials.

remotes/origin/feature/the-main-menu
fdai7207 2 years ago
parent
commit
509362d0aa
  1. BIN
      src/.employeeLogin.c.swp
  2. 53
      src/employeeLogin.c

BIN
src/.employeeLogin.c.swp

53
src/employeeLogin.c

@ -4,8 +4,8 @@
extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword)
{
char listUsername[credentialLength];
char listPassword[credentialLength];
char* listUsername = malloc(credentialLength * sizeof(char*));
char* listPassword = malloc(credentialLength * sizeof(char*));
FILE* employeeList = fopen("src/employeeList.txt","r");
@ -16,14 +16,17 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword)
}
else
{
/*loop that checks if the two strings seperated by space exist in the employee list*/
while (fscanf(employeeList, "%s %s", listUsername, listPassword) != EOF)
{
if (strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) == 0)
if (!(strcmp(inputUsername, listUsername)) && !(strcmp(inputPassword, listPassword)))
{
fclose(employeeList);
return 1;
}
else if(strcmp(inputUsername, listUsername) == 0 && strcmp(inputPassword, listPassword) != 0)
else if(!(strcmp(inputUsername, listUsername)) && strcmp(inputPassword, listPassword))
{
fclose(employeeList);
return 2;
@ -32,46 +35,56 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword)
fclose(employeeList);
return 0;
}
free(inputUsername);
free(inputPassword);
}
void employeeCredentials(char* username,char* password)
void employeeCredentials(char* inputUsername,char* inputPassword)
{
printf("Enter username: ");
scanf("%s", username);
scanf("%s", inputUsername);
printf("Enter password: ");
scanf("%s", password);
scanf("%s", inputPassword);
}
void loginAsEmployee()
{
int counter=2;
char username[credentialLength];
char password[credentialLength];
employeeCredentials(username, password);
int counter=3;
char* username = malloc(credentialLength * sizeof(char*));
char* password = malloc(credentialLength * sizeof(char*));
while(counter>0)
{
if(checkEmployeeCredentials(username, password) == 0)
{
printf("User not found\n");
employeeCredentials(username, password);
int checkCredentials = checkEmployeeCredentials(username,password);
if(checkCredentials == 0)
{
printf("\n\nUser not found\n\n");
}
else if(checkEmployeeCredentials(username, password)==2)
else if(checkCredentials == 2)
{
printf("Wrong Informations !\nyou have %d tries left\n",counter);
employeeCredentials(username, password);
printf("\n\nWrong Informations !\nyou have %d tries left\n\n",counter-1);
--counter;
}
else
{
printf("User Approved\n");
printf("\n\nUser Approved\n\n");
break;
}
}
if(counter==0)
{
printf("you used up all of the tries! account locked\n");
printf("you used up all of the tries! account locked\nPlease contact an employee of higher clearance !\n\n");
}
free(username);
free(password);
}
Loading…
Cancel
Save