Skip to content

Commit

Permalink
Implement prompt function
Browse files Browse the repository at this point in the history
  • Loading branch information
speelbarrow committed Jun 19, 2023
1 parent e45d961 commit 8b0e1b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/amadeus/prompt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef PROMPT_H
#define PROMPT_H

int prompt(const char **message);

#endif // PROMPT_H
29 changes: 29 additions & 0 deletions src/prompt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <AMaDEUS/prompt.h>

#include <stdio.h>
#include <stdlib.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) {
printf("%s: ", *message);

char *buffer = NULL;
size_t buffer_size = 0;
getline(&buffer, &buffer_size, stdin);

int result = atoi(buffer);
if (result != 0) {
return result;
} else if (++count < 3) {
printf("Invalid input. Please try again.\n");
return prompt_counted(message, count);
} else {
printf("Too many invalid inputs. Exiting.\n");
exit(1);
}
}

0 comments on commit 8b0e1b9

Please sign in to comment.