Compare commits
No commits in common. "1b99edad974e6544b0992d932a511628480c8d4e" and "181a0f3dcd51091b95e84695b8ad8d8ce7bcd722" have entirely different histories.
1b99edad97
...
181a0f3dcd
@ -29,24 +29,16 @@ int main() {
|
|||||||
|
|
||||||
printf("%s\n", PROMPT);
|
printf("%s\n", PROMPT);
|
||||||
|
|
||||||
// Use getline for dynamic allocation
|
|
||||||
ssize_t chars_read = getline(&buf, &len, stdin);
|
ssize_t chars_read = getline(&buf, &len, stdin);
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
long input = strtol(buf, &end, 10);
|
long input = strtol(buf, &end, 10);
|
||||||
|
|
||||||
// Check whether a range error occured or
|
if(errno == ERANGE || chars_read == -1 || end == &buf[0]) {
|
||||||
// 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);
|
printf("%s\n", NOT_A_NUMBER);
|
||||||
return EXIT_FAILURE;
|
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') {
|
if(*end != '\n') {
|
||||||
printf("%s\n", INVALID_NUMBER);
|
printf("%s\n", INVALID_NUMBER);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -60,6 +52,5 @@ int main() {
|
|||||||
printf("%s\n", INPUT_OKAY);
|
printf("%s\n", INPUT_OKAY);
|
||||||
printf("%s\n", QUOTES[input]);
|
printf("%s\n", QUOTES[input]);
|
||||||
|
|
||||||
free(buf);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user