Browse Source

refactoring: created printInvalidNumberMessage function

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
7a7fceb464
  1. 42
      src/userinput.c

42
src/userinput.c

@ -147,25 +147,7 @@ bool isNumberValid(long double value, long double *min, long double *max, long d
return true;
}
long double getNumber(char *message, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) {
while (true) {
char *input = gets(message, NULL, NULL);
if (strlen(input) == 0) {
printText("Fehlende Eingabe!\n");
free(input);
continue;
}
char *endptr;
long double number = strtold(input, &endptr);
if (strlen(endptr) > 0) {
printText("Ungueltige Eingabe! Gib eine gueltige Zahl ein.\n");
free(input);
continue;
}
free(input);
if (isNumberValid(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder)) {
return number;
}
void printInvalidNumberMessage(long double value, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) {
printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");
if (min != NULL) {
printText(" >=");
@ -192,6 +174,28 @@ long double getNumber(char *message, long double *min, long double *max, long do
printlf(rightBorder);
}
printText("\n");
}
long double getNumber(char *message, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) {
while (true) {
char *input = gets(message, NULL, NULL);
if (strlen(input) == 0) {
printText("Fehlende Eingabe!\n");
free(input);
continue;
}
char *endptr;
long double number = strtold(input, &endptr);
if (strlen(endptr) > 0) {
printText("Ungueltige Eingabe! Gib eine gueltige Zahl ein.\n");
free(input);
continue;
}
free(input);
if (isNumberValid(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder)) {
return number;
}
printInvalidNumberMessage(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder);
}
}

Loading…
Cancel
Save