Compare commits
3 Commits
181a0f3dcd
...
1b99edad97
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1b99edad97 | ||
![]() |
d9db45094b | ||
![]() |
a1bac88d7d |
@ -29,16 +29,24 @@ int main() {
|
||||
|
||||
printf("%s\n", PROMPT);
|
||||
|
||||
// Use getline for dynamic allocation
|
||||
ssize_t chars_read = getline(&buf, &len, stdin);
|
||||
|
||||
errno = 0;
|
||||
long input = strtol(buf, &end, 10);
|
||||
|
||||
if(errno == ERANGE || chars_read == -1 || end == &buf[0]) {
|
||||
// Check whether a range error occured or
|
||||
// No characters were read by getline
|
||||
// or check that strtol didnt move a single char
|
||||
if(errno == ERANGE || chars_read == -1 || end == buf) {
|
||||
printf("%s\n", NOT_A_NUMBER);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// getline always adds the \n to the result string
|
||||
// If the ending char by strtol is not that newline
|
||||
// It means the string is not a whole number
|
||||
// (e.g. 12abc, 1.23)
|
||||
if(*end != '\n') {
|
||||
printf("%s\n", INVALID_NUMBER);
|
||||
return EXIT_FAILURE;
|
||||
@ -52,5 +60,6 @@ int main() {
|
||||
printf("%s\n", INPUT_OKAY);
|
||||
printf("%s\n", QUOTES[input]);
|
||||
|
||||
free(buf);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user