-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e45d961
commit 8b0e1b9
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |