From 239679e77aaf0857b16852d5fa2bbaf8d5aef889 Mon Sep 17 00:00:00 2001 From: TheUltimateOptimist Date: Sun, 28 Jan 2024 00:51:13 +0100 Subject: [PATCH] implemented getstr --- src/userinput.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/userinput.c b/src/userinput.c index 8fe6660..ec1a8f1 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -3,6 +3,7 @@ #include #include #include +#include void trimLeft(char *input) { size_t length = strlen(input); @@ -46,4 +47,26 @@ char *readInput() { trimLeft(buffer); trimRight(buffer); return buffer; +} + +char *getstr(char *message, unsigned long *minLength, unsigned long *maxLength) { + printf("%s", message); + fflush(stdout); + char *result = readInput(); + while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) { + printf("%s", "Ungueltige Eingabe! "); + if (minLength != NULL && maxLength == NULL) { + printf("Die Eingabe muss mind. %lu Zeichen lang sein.\n", *minLength); + } + else if (maxLength != NULL && minLength == NULL) { + printf("Die Eingabe darf maximal %lu Zeichen lang sein.\n", *maxLength); + } + else { + printf("Die Eingabe muss %lu bis %lu Zeichen lang sein.\n", *minLength, *maxLength); + } + printf("%s", message); + fflush(stdout); + result = readInput(); + } + return result; } \ No newline at end of file