Browse Source

Modification of the function collectCustomerProperties. It now contains a call to balanceToDouble(). The reason is: The function scanf() is eliminated. Instead, the input is completely handled by getchar(). Each character is stored consecutively. Thus it can be the parameter for balanceToDouble().

remotes/origin/development
fdai7057 2 years ago
parent
commit
58704be59a
  1. 39
      src/createCustomer.c
  2. 2
      src/customerProperties.h
  3. 4
      src/error.c
  4. 4
      src/helperFunctions.c
  5. 1
      test/test_helperFunctions.c

39
src/createCustomer.c

@ -18,7 +18,7 @@ void collectCustomerProperties()
int letterCounter = 0; int letterCounter = 0;
int letterMaximum = 15; int letterMaximum = 15;
char userInput=' '; char userInput=' ';
bool inputTooLong = false;
bool inputTooLong = false, foundComma = false;
printf("To create a new user, enter the information required below.\n"); printf("To create a new user, enter the information required below.\n");
printf("Enter forename (max. 15 letters):\n"); printf("Enter forename (max. 15 letters):\n");
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){
@ -58,6 +58,7 @@ void collectCustomerProperties()
errorMessage(-11); errorMessage(-11);
} }
printf("Enter password (max. 20 letters):\n"); printf("Enter password (max. 20 letters):\n");
letterMaximum = 20;
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){
*(instance.password+letterCounter) = userInput; *(instance.password+letterCounter) = userInput;
++letterCounter; ++letterCounter;
@ -68,12 +69,40 @@ void collectCustomerProperties()
} }
if(inputTooLong){ if(inputTooLong){
errorMessage(-9); errorMessage(-9);
}
}else{
*(instance.password+letterCounter) = '\0'; *(instance.password+letterCounter) = '\0';
letterCounter = 0;
printf("Enter balance:\n");
scanf("%f",&instance.balance);
}
letterCounter = 0;
printf("Enter balance (max. 10 digits):\n");
char *balanceCharacters = calloc(10+1+1+1,sizeof(char));
letterMaximum = 10;
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){
*(balanceCharacters+letterCounter) = userInput;
++letterCounter;
if(letterCounter>=letterMaximum){
inputTooLong = true;
break;
}
}
if(inputTooLong){
puts("Input too long.");
exit(-1);
//call errorMessage(-10);
}else{
if(!foundComma){
*(balanceCharacters+letterCounter) = '.';
++letterCounter;
*(balanceCharacters+letterCounter) = '0';
++letterCounter;
}
*(balanceCharacters+letterCounter) = '\0';
}
if(!everyCharacterIsDigit(balanceCharacters)){
puts("You have entered a character that is not a number. This is not allowed. Aborting!");
exit(-1);
}
instance.balance = balanceToDouble(balanceCharacters);
printf("Account successfully created. Your ID is: %d. Note it somewhere!\n",instance.ID); printf("Account successfully created. Your ID is: %d. Note it somewhere!\n",instance.ID);
customer_t *referenceToCustomerInstance = &instance; customer_t *referenceToCustomerInstance = &instance;
writeCustomerPropertiesToFile(referenceToCustomerInstance); writeCustomerPropertiesToFile(referenceToCustomerInstance);

2
src/customerProperties.h

@ -6,6 +6,6 @@ typedef struct Customer
char *IDAsString; char *IDAsString;
char *password; char *password;
char *forename, *surname; char *forename, *surname;
float balance;
double balance;
}customer_t; }customer_t;
#endif #endif

4
src/error.c

@ -46,12 +46,12 @@ int errorMessage(int errorCode)
exit(-1); exit(-1);
break; break;
case -10: case -10:
puts("You have entered a digit for your forename. This is not allowed. Aborting!");
puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!");
returnValue = -10; returnValue = -10;
exit(-1); exit(-1);
break; break;
case -11: case -11:
puts("You have entered a digit for your surname. This is not allowed. Aborting!");
puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!");
returnValue = -11; returnValue = -11;
exit(-1); exit(-1);
break; break;

4
src/helperFunctions.c

@ -71,7 +71,7 @@ bool everyCharacterIsDigit(char *string)
{ {
bool onlyDigits = true; bool onlyDigits = true;
for(int i=0;*(string+i)!='\0';++i){ for(int i=0;*(string+i)!='\0';++i){
if(*(string+i)<'0'||*(string+i)>'9'){
if( !(*(string+i)>='0'&&*(string+i)<='9') && *(string+i)!='.'){
onlyDigits = false; onlyDigits = false;
break; break;
} }
@ -82,7 +82,7 @@ bool everyCharacterIsDigit(char *string)
bool isLetterOfAlphabet(char *string){ bool isLetterOfAlphabet(char *string){
bool everyCharIsInAlphabet = true; bool everyCharIsInAlphabet = true;
for(int i=0;*(string+i)!='\0';++i){ for(int i=0;*(string+i)!='\0';++i){
if( !(*(string+i)>='A'&&*(string+i)<='Z') && !(*(string+i)>='a'&&*(string+i)<='z')){
if(!(*(string+i)>='A'&&*(string+i)<='Z') && !(*(string+i)>='a'&&*(string+i)<='z')){
everyCharIsInAlphabet = false; everyCharIsInAlphabet = false;
break; break;
} }

1
test/test_helperFunctions.c

@ -2,7 +2,6 @@
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <unity.h> #include <unity.h>
#include "unity_config.h"
#include "../src/helperFunctions.c" #include "../src/helperFunctions.c"
void test_isLetterOfAlphabet() void test_isLetterOfAlphabet()

Loading…
Cancel
Save