From 34007afda00d115ddcffeb83ca41e35b82cb627b Mon Sep 17 00:00:00 2001 From: fdai7057 Date: Wed, 1 Feb 2023 12:17:45 +0100 Subject: [PATCH] Implementation of to_unsignedInteger() --- src/CustomerProperties.h | 14 ------ src/LoginCustomer.c | 97 ---------------------------------------- src/LoginCustomer.h | 10 ----- src/stringManipulation.c | 11 +++++ src/stringManipulation.h | 2 + 5 files changed, 13 insertions(+), 121 deletions(-) delete mode 100644 src/CustomerProperties.h delete mode 100644 src/LoginCustomer.c delete mode 100644 src/LoginCustomer.h diff --git a/src/CustomerProperties.h b/src/CustomerProperties.h deleted file mode 100644 index 1972e12..0000000 --- a/src/CustomerProperties.h +++ /dev/null @@ -1,14 +0,0 @@ -typedef struct Customer{ -<<<<<<< HEAD -<<<<<<< HEAD - int ID; - char *forename,*surname,*password; -======= - char *ID, *forename, *surname, *password; ->>>>>>> 3e7a7d3 (Implementation of functions LoginAsCustomer() and Login()) -======= - int ID; - char *forename,*surname,*password; ->>>>>>> 4d810f5640d89a86c1f2feef3ab19118518ad49f - float balance; -}customer_t; diff --git a/src/LoginCustomer.c b/src/LoginCustomer.c deleted file mode 100644 index 5c2a7a4..0000000 --- a/src/LoginCustomer.c +++ /dev/null @@ -1,97 +0,0 @@ -#include "LoginCustomer.h" - -bool checkLogin(bool loginSuccessful) -{ - return (loginSuccessful) ? true : false; -} - -void collectCustomerDataForLogin(int attempts) -{ - customer_t c; - c.ID = calloc(15+1,sizeof(char)); - c.password = calloc(15+1, sizeof(char)); - char digitCharacterFromUser, passwordCharacterFromUser; - int IDLengthCounter = 0, passwordLengthCounter = 0; - const int IDMaxLength = 16, passwordMaxLength = 16; - - printf("Enter ID:\n"); - while(((digitCharacterFromUser=getchar()) != '\n')&&IDLengthCounter='0'&&digitCharacterFromUser<='9'){ - *(c.ID+IDLengthCounter) = digitCharacterFromUser; - } - else{ - printf("Character entered is not a digit. Aborting.\n"); - exit(-1); - } - ++IDLengthCounter; - } - *(c.ID+IDLengthCounter) = '\0'; - - if(IDLengthCounter>=IDMaxLength){ - printf("ID entered is too long. Aborting.\n"); - exit(-1); - } - - printf("Enter password:\n"); - while((passwordCharacterFromUser=getchar())!='\n'&&passwordLengthCounter=passwordMaxLength){ - printf("Password entered is too long. Aborting.\n"); - exit(-1); - } - customer_t *ptr = &c; - bool loginSuccessful = loginCustomer(ptr); - if(loginSuccessful ) { - printf("Welcome to the menu!\n"); - //call menu() - }else if(!loginSuccessful && attempts < MAX_LOGIN_ATTEMPTS){ - printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts); - collectCustomerDataForLogin(++attempts); - }else{ - printf("Maximum number of attempts reached. Program terminates.\n"); - exit(-1); - //call error() - } - -} - -bool loginCustomer(customer_t *c) -{ - bool foundCustomerEntryInFile = false; - char *searchForThisString = generateCheckString(c->ID,c->password); - char *lineFromCustomerFile = calloc(40,sizeof(char)); - FILE *readCustomerFile = fopen("CustomerData.txt", "r"); - if(readCustomerFile==NULL){ - printf("Could not find file. Aborting.\n"); - exit(-1); - //call error() - } - while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ - if(strcmp(searchForThisString,lineFromCustomerFile)==0){ - foundCustomerEntryInFile = true; - break; - } - } - - if(checkLogin(foundCustomerEntryInFile)){ - printf("Login successful.\n"); - return foundCustomerEntryInFile; - }else{ - printf("Login not successful.\n"); - } - fclose(readCustomerFile); - return foundCustomerEntryInFile; -} - -char *generateCheckString(char *ID, char *password){ - int checkStringLength = strlen(ID) + 1 + strlen(password) + 1; - char *checkString = calloc(checkStringLength, sizeof(char)); - checkString = strcat(ID,"="); - checkString = strcat(checkString,password); - *(checkString+checkStringLength) = '\0'; - return checkString; -} diff --git a/src/LoginCustomer.h b/src/LoginCustomer.h deleted file mode 100644 index 809765b..0000000 --- a/src/LoginCustomer.h +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include -#include -#include "CustomerProperties.h" -#define MAX_LOGIN_ATTEMPTS 3 -char *generateCheckString(char *, char*); -bool checkLogin(bool); -void collectCustomerDataForLogin(int); -bool loginCustomer(customer_t *); diff --git a/src/stringManipulation.c b/src/stringManipulation.c index 8ac683a..eb105cd 100644 --- a/src/stringManipulation.c +++ b/src/stringManipulation.c @@ -54,6 +54,17 @@ char *to_string(int number) } } +unsigned int to_unsignedInteger(char *ID) +{ + unsigned int result = 0; + int IDLength = strlen(ID); + for(int i=0, j = IDLength - 1;i #include #include +#include char *stringConcatenation(char *, char *); char *to_string(int); char *generateCheckString(int, char *); +unsigned int to_unsignedInteger(char *);