Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Diff needed for Shakespeare's int_input to behave like scanf
diff --git a/spl-1.2.1/libspl.c b/spl-1.2.1/libspl.c
index ad597fe..1e0bd13 100644
--- a/spl-1.2.1/libspl.c
+++ b/spl-1.2.1/libspl.c
@@ -305,40 +305,28 @@ int int_factorial(int line, int n)
return n;
}
void int_input(int line, CHARACTER *character)
{
- char buf[BUF_SIZE];
long lval;
+ int succeeded;
/* Make sure the character isn't NULL */
if (character == NULL) {
if (num_on_stage == 1) {
runtime_error(line, newstr("Erroneous use of second person pronoun. There is only one character on stage!"));
} else {
runtime_error(line, newstr("Ambiguous use of second person pronoun. There are more than two characters on stage!"));
}
}
- /* Get a line of text */
- fgets(buf, BUF_SIZE, stdin);
-
- /* Try to parse that into an integer */
- errno = 0;
- lval = strtol(buf, NULL, 10);
- if (lval == 0) {
- switch (errno) {
- case EINVAL:
- runtime_error(line, cat2(newstr(character->name), newstr("'s heart whispered something that was not a valid integer.")));
- break;
- case ERANGE:
- runtime_error(line, cat2(newstr(character->name), newstr("'s heart whispered an integer that was out of range.")));
- break;
- default:
- /* No error, buf really contained the integer zero */
- break;
- }
+ succeeded = scanf("%ld", &lval);
+ if (succeeded != 1) {
+#ifdef DEBUG
+ fprintf(stderr, "\n%s failed to read an integer and retained the value %d\n", character->name, character->value);
+#endif
+ return;
}
/* Make sure it was not out of range */
if (lval < INT_MIN || lval > INT_MAX) {
runtime_error(line, cat2(newstr(character->name), newstr("'s heart whispered an integer that was out of range.")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment