From 509362d0aa60dce0ea5bea5b08b9d1c3a8db68d9 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 1 Feb 2023 19:46:39 +0100 Subject: [PATCH] refactoring: changed some variable names for readability and used pointers to conserve memory in the functions CheckEmployeeCredentials() and EmployeeCredentials. --- src/.employeeLogin.c.swp | Bin 0 -> 12288 bytes src/employeeLogin.c | 55 ++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/.employeeLogin.c.swp diff --git a/src/.employeeLogin.c.swp b/src/.employeeLogin.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..a2975fcb31a1a48c8e9e0e9a4ff11c9c866b60ce GIT binary patch literal 12288 zcmeI2O>Epm6vw9(6_n z=2=m_G;wiuj?RotFti64yYlh(_6JWe_TwmHE?-Yv-c?U-+s-x@&Od))zJ7Lc?wO}% zXZ9R>H8(iE#N~3AOWqM(A(MK%9w(wZ9!K`L=!SldhqkvJsC}0#Y3K#{90!D#5Pyya%?B*fH7Vd`g`2Tb_6>LqTmzfn5_k@bf``FxhZ*|`Tn8KAWv~ofFbynl0z3pB1b2bI zq32)V4e&bH0$0IH;38NAO7kUPno+=Re)UHeyeZ5#j zl6M9A($0LmniEEzOdPAc9`p#q+SCh)`#uE@C9a?(@CfoYMAz%MA|kReD&o=>G; mSw&CM(Tv;SgBDFH5wBY?U?dOwM~{@ z@_&{h!I!gubhma~SzbhZG!uqVu%f)nmMU$qlJH~EH#BUrUe8zG+w2QbAUiZGognJ+ z#0zAs0#Qo2d_~a7b|w$`!b#FqYq`n#WUjt)Hj7r>DQ`%H?FxJO=}5HkDWb>ym>Lb5 zsxUP~wP8PZ#3_S>9PCCv&}`P=LVK(Ml}l=HrnS1m-qTKRvaV+SVdQ|LmQJMFK$bCO zNHv`TyH22i8ESB*%Bt)6LZ`u#Ity%NF@JcFb4FoF7*8e0aW&Dx07Cwwv&o2&qHbU~QnU@1zbRtsT4hUp&xu z^f4~-G!jWKk~C2qCCB!ISXkP#mKP@jN1~XiA{_pudUAVHg+0})+U@PC5m{oXJJs}& z8?oXfvb&)*tO)sVtRGi}PMxB`aayHXI!^zp6h~Dpv8Vf!|J{AhA?x3}K0H#pyWRN; zG39nz2}>w`~j1NQ0Ue&gB)oG+yc>gS|Qyg-49BowfTifodk!P={nu`J20xd9?~vw;h#%PVGKY fiH&;F_IqtX<-Ts|m-KFV;8B-*a!$zJ7<&H!zLM@d literal 0 HcmV?d00001 diff --git a/src/employeeLogin.c b/src/employeeLogin.c index 624d0ae..0d44ddf 100644 --- a/src/employeeLogin.c +++ b/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"); @@ -15,15 +15,18 @@ extern int checkEmployeeCredentials(char *inputUsername, char *inputPassword) exit(1); } 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) + employeeCredentials(username, password); + + int checkCredentials = checkEmployeeCredentials(username,password); + + + if(checkCredentials == 0) { - printf("User not found\n"); - employeeCredentials(username, password); + 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); + }