Browse Source

implemented getstr

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
239679e77a
  1. 23
      src/userinput.c

23
src/userinput.c

@ -3,6 +3,7 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void trimLeft(char *input) {
size_t length = strlen(input);
@ -47,3 +48,25 @@ char *readInput() {
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;
}
Loading…
Cancel
Save