From b02f8ebb67987ff7bad9b3e4bd69589dfc79c17e Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 29 Jan 2023 00:25:24 +0100 Subject: [PATCH] refactoring: changed some variable names, made code more readable and enhanced some functions --- src/mainMenu.c | 59 ++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/mainMenu.c b/src/mainMenu.c index 9cd19c0..dd249fc 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -1,62 +1,44 @@ #include"mainMenu.h" -bool agePermission(int age){ +bool agePermission(int age) +{ - if(age >= 18) - { - - return true; - - } - - else - { - - return false; - - } + return age >= 18; } -bool checkIfInteger(char* input){ - - char *end_pointer; - - strtol(input, &end_pointer, 10); - - if (end_pointer == input || *end_pointer != '\0') - { +bool checkIfInteger(char* userInput) +{ - return false; + char *endPointer; - } + /*the endPointer points to the first non-integer character signaled to stop conversion*/ - else - { + strtol(userInput, &endPointer, 10); - return true; + return !(endPointer == userInput || *endPointer != '\0'); - } } -void ageInput(){ +void ageInput() +{ - char input[20]; - char* input_pointer; + char* userInput = malloc(20*sizeof(char*)); + char* userInputPointer; long age; printf("\nPlease specify your age : "); - scanf("%s",input); + scanf("%s",userInput); while (true) { - age = strtol(input,&input_pointer,10); + age = strtol(userInput,&userInputPointer,10); - if(checkIfInteger(input) == true && agePermission(age)== true) + if((checkIfInteger(userInput))&& (agePermission(age))) { - age = strtol(input,&input_pointer,10); + //age = strtol(userInput,&userInputPointer,10); printf("Access granted!\n\n\n\n"); @@ -66,7 +48,7 @@ void ageInput(){ } - else if(checkIfInteger(input) == true && agePermission(age)== false) + else if((checkIfInteger(userInput)) && !(agePermission(age))) { printf("You should be at least 18 years old to create a bank account!\n"); @@ -79,7 +61,7 @@ void ageInput(){ { printf("input invalid! try again!\n"); - scanf("%s",input); + scanf("%s",userInput); } @@ -88,7 +70,8 @@ void ageInput(){ } -void showMenu(){ +void showMenu() +{ printf("\t\t\t\t\t\t\t Welcome to Bank Manager!"); printf("\n\n\n\n\t\t\t\t\t\tPlease select one of the following functions!");