coding-horror/basic-computer-games · error
Error Getting your input
Error message
Error Getting your input
What it means
In 47_Hi-Lo, after each jackpot round the game asks 'PLAY AGAIN (YES OR NO)?' and reads the response via io::stdin().read_line(&mut tocontinue).expect("Error Getting your input"). The response is compared case-insensitively to 'YES' to decide whether to continue. The .expect() panics on stdin I/O failure.
Source
Thrown at 47_Hi-Lo/rust/src/main.rs:63
println!("YOUR GUESS IS TOO LOW.\n");
} else {
println!("YOUR GUESS IS TOO HIGH.\n");
}
// if 6 tries are over make total jackpot amount to zero
if i == 5 {
total = 0;
println!(
"YOU BLEW IT...TOO BAD...THE NUMBER WAS {}\n",
jackpot_amount
);
}
}
println!("PLAY AGAIN (YES OR NO)?");
let mut tocontinue = String::new();
io::stdin()
.read_line(&mut tocontinue)
.expect("Error Getting your input");
let tocontinue = tocontinue.trim().to_ascii_uppercase();
if tocontinue.eq("YES") {
println!("\n");
continue;
} else {
println!("\nSO LONG. HOPE YOU ENJOYED YOURSELF!!!\n");
break;
}
}
}
View on GitHub (pinned to 5301155192)
Solutions
- Replace .expect() with .unwrap_or_default() — an empty string won't equal 'YES', so the game correctly prints 'SO LONG' and exits
- Use if read_line(...).is_err() to break the outer loop
- Return early from the function on Err
Example fix
// before
let mut tocontinue = String::new();
io::stdin()
.read_line(&mut tocontinue)
.expect("Error Getting your input");
let tocontinue = tocontinue.trim().to_ascii_uppercase();
// after
let mut tocontinue = String::new();
let _ = io::stdin().read_line(&mut tocontinue);
let tocontinue = tocontinue.trim().to_ascii_uppercase(); Defensive patterns
Strategy: fallback
Try / catch
let mut tocontinue = String::new(); let _ = io::stdin().read_line(&mut tocontinue); // empty string != "YES", so the game prints 'SO LONG' and exits, // which is the correct response to EOF at this prompt
Prevention
- For yes/no prompts where non-yes already means exit, I/O failure should produce the same exit — use let _ = instead of .expect()
- Recognize that EOF at a 'play again?' prompt is the user's way of saying 'no more' — treat it accordingly
- Avoid .expect() on the final input in a game session where the natural response to EOF is 'stop playing'
When it happens
Trigger: The game finishes a round and prompts to continue, but stdin is at EOF or the read fails. A non-'YES' answer already triggers the 'SO LONG' exit, so EOF should logically behave the same way.
Common situations: Piped input that ends after a game round, pressing Ctrl+D at the play-again prompt, or automated tests that stop feeding input after one game.
Related errors
- Error reading from stdin
- Error reading line.
- ~~Failed reading line!~~
- Failed to get Input
- Failed to read line
AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13).
Data as JSON: /api/errors/a0f1ca3bc781d5ca.
Report an issue: GitHub.