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; 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:"); printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");
if (min != NULL) { if (min != NULL) {
printText(" >="); printText(" >=");
@ -193,6 +175,28 @@ long double getNumber(char *message, long double *min, long double *max, long do
} }
printText("\n"); 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);
}
} }
short gethd(char *message, short *min, short *max) { short gethd(char *message, short *min, short *max) {

Loading…
Cancel
Save