diff --git a/src/userinput.c b/src/userinput.c index 9e7e51c..98fc020 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -125,6 +125,28 @@ char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) { return result; } +bool isNumberValid(long double value, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) { + if (min != NULL && value < *min) { + return false; + } + if (max != NULL && value > *max) { + return false; + } + if (greaterThan != NULL && !(value > *greaterThan)) { + return false; + } + if (smallerThan != NULL && !(value < *smallerThan)) { + return false; + } + if (value < leftBorder) { + return false; + } + if (value > rightBorder) { + return false; + } + 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); @@ -141,26 +163,7 @@ long double getNumber(char *message, long double *min, long double *max, long do continue; } free(input); - bool isValid = true; - if (min != NULL && number < *min) { - isValid = false; - } - if (max != NULL && number > *max) { - isValid = false; - } - if (greaterThan != NULL && !(number > *greaterThan)) { - isValid = false; - } - if (smallerThan != NULL && !(number < *smallerThan)) { - isValid = false; - } - if (number < leftBorder) { - isValid = false; - } - if (number > rightBorder) { - isValid = false; - } - if (isValid) { + if (isNumberValid(number, min, max, greaterThan, smallerThan, leftBorder, rightBorder)) { return number; } printText("Ungueltige Eingabe! Fuer die eingegebene Zahl muss gelten:");