Skip to content

Commit

Permalink
Add range based prompt functions
Browse files Browse the repository at this point in the history
  • Loading branch information
speelbarrow committed Jun 20, 2023
1 parent 123ab87 commit 634b59c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 3 additions & 0 deletions include/amadeus/prompt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
#define PROMPT_H

int prompt(const char *message);
int prompt_min(const char *message, int min);
int prompt_max(const char *message, int max);
int prompt_range(const char *message, int min, int max);

#endif // PROMPT_H
17 changes: 9 additions & 8 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int prompt_counted(const char *message, int count);

int prompt(const char *message) {
return prompt_counted(message, 0);
}

int prompt_counted(const char *message, int count) {
int prompt_counted(const char *message, int min, int max, int count) {
printf("%s: ", message);

char *buffer = NULL;
Expand All @@ -21,9 +16,15 @@ int prompt_counted(const char *message, int count) {
return result;
} else if (++count < 3) {
printf("Invalid input. Please try again.\n");
return prompt_counted(message, count);
return prompt_counted(message, min, max, count);
} else {
printf("Too many invalid inputs. Exiting.\n");
exit(1);
}
}
int prompt_counted(const char *message, int min, int max, int count);

int prompt(const char *message) { return prompt_range(message, -INFINITY, INFINITY); }
int prompt_min(const char *message, int min) { return prompt_range(message, min, INFINITY); }
int prompt_max(const char *message, int max) { return prompt_range(message, -INFINITY, max); }
int prompt_range(const char *message, int min, int max) { return prompt_counted(message, min, max, 0); }

0 comments on commit 634b59c

Please sign in to comment.