From 1d3092544fa74ffa5014263d1654c21d0fd1b813 Mon Sep 17 00:00:00 2001 From: fdai7057 Date: Mon, 6 Feb 2023 13:48:28 +0100 Subject: [PATCH] Add new cases inside the switch of the function errorMessage(). Remove the call to the function exit() inside errorMessage(). It is now done by the function that calls it. Now the different return values of this function make sense. --- src/createCustomer.c | 23 ++++++++++++++++------- src/error.c | 10 +--------- src/loginCustomer.c | 12 +++++++----- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/createCustomer.c b/src/createCustomer.c index af52cb3..9d3a93a 100644 --- a/src/createCustomer.c +++ b/src/createCustomer.c @@ -17,6 +17,7 @@ void collectCustomerProperties() instance.ID = generateID(); int letterCounter = 0; int letterMaximum = 15; + int errorResult = 0; char userInput=' '; bool inputTooLong = false, foundComma = false; printf("To create a new user, enter the information required below.\n"); @@ -30,14 +31,16 @@ void collectCustomerProperties() } } if(inputTooLong){ - errorMessage(-7); + errorResult = errorMessage(-7); + if(errorResult==-7) exit(-1); } else{ *(instance.forename+letterCounter) = '\0'; letterCounter = 0; } if(!isLetterOfAlphabet(instance.forename)){ - errorMessage(-10); + errorResult = errorMessage(-10); + exit(-1); } printf("Enter surname (max. 15 letters):\n"); while(letterCounterID, referenceToCustomerInstance->password),referenceToCustomerInstance->ID,referenceToCustomerInstance->forename,referenceToCustomerInstance->surname,referenceToCustomerInstance->password, referenceToCustomerInstance->balance); fclose(customerData); } else{ - errorMessage(-6); + errorResult = errorMessage(-6); + if(errorResult==-6) exit(-1); } } diff --git a/src/error.c b/src/error.c index 3765a32..26821c1 100644 --- a/src/error.c +++ b/src/error.c @@ -17,9 +17,8 @@ int errorMessage(int errorCode) returnValue = -3; break; case -4: - puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Aborting!"); + puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Without users there is no file. Aborting!"); returnValue = -4; - exit(-1); break; case -5: puts("You should be at least 18 years old to create a bank account!"); @@ -28,37 +27,30 @@ int errorMessage(int errorCode) case -6: puts("Error when trying to open a file to create a customer account."); returnValue = -6; - exit(-1); break; case -7: puts("Forename too long. (length > 15 characters) Aborting!"); returnValue = -7; - exit(-1); break; case -8: puts("Surname too long. (length > 15 characters) Aborting!"); returnValue = -8; - exit(-1); break; case -9: puts("Password too long. (length > 20 characters) Aboring!"); returnValue = -9; - exit(-1); break; case -10: puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!"); returnValue = -10; - exit(-1); break; case -11: puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!"); returnValue = -11; - exit(-1); break; case -12: puts("You entered too many digits."); returnValue = -12; - exit(-1); break; default: puts("Error code unknown."); diff --git a/src/loginCustomer.c b/src/loginCustomer.c index 74de573..f06873c 100644 --- a/src/loginCustomer.c +++ b/src/loginCustomer.c @@ -12,6 +12,7 @@ void collectCustomerDataForLogin(int attempts) c.password = calloc(20+1, sizeof(char)); int digitCharacterFromUser, passwordCharacterFromUser; int IDLengthCounter = 0, passwordLengthCounter = 0; + int errorResult = 0; const int IDMaxLength = 16, passwordMaxLength = 21; printf("Enter ID:\n"); while((digitCharacterFromUser=getchar())!='\n'&&IDLengthCounterIDAsString); char *searchForThisString = generateCheckString(IDAsNumber,c->password); char *lineFromCustomerFile = calloc(40,sizeof(char)); + int errorResult = 0; FILE *readCustomerFile = fopen("CustomerData.txt", "r"); if(readCustomerFile==NULL){ - errorMessage(-4); + errorResult = errorMessage(-4); + if(errorResult==-4) exit(-1); } while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ if(strcmp(searchForThisString,lineFromCustomerFile)==0){ @@ -77,7 +79,7 @@ bool loginCustomer(customer_t *c) fclose(readCustomerFile); return foundCustomerEntryInFile; }else{ - errorMessage(-1); + errorResult = errorMessage(-1); } fclose(readCustomerFile); return foundCustomerEntryInFile;