Help with ParseIntError{ kind : Empty)

// Hello,
I am trying to write a small program for learning sake that creates a sort of menu where the user can select different texts by pressing the corresponding number.
The text then displays, and on hitting Enter the program returns to the menu.
Now that last part delivers an error. ParseIntError {kind : Empty}
Please see the playground link.

Any suggestions are welcome,

Cheerio

It appears that the Rust Playground doesn't support stdin. Your program seems fine, but it's acting like you hit enter without typing a number, which causes the expected panic.

Wouldn't that cause the readable text to not be loaded in the first place?

I'm not sure what you mean exactly, but stdout is working fine, so everything will print. It stumbles at these lines:

io::stdin().read_line(&mut x).expect("Failed to read line");
let mut x : i32 = x.trim().parse().expect("Please type a number!");

It reads a blank line, then parse() panics.

In this case, when it tries to read from stdin it immediately receives an end-of-file marker, so the read_line call returns immediately with an empty string. The empty string is not a valid integer.

Note: When your formatting style is this far from the standard Rust formatting style, please hit the rustfmt button under tools in the playground before posting the question. That makes it a lot easier for us to read your code, and hence also easier to help you.

1 Like

@zthompson47
So if I am getting this right: The problem is that when I press enter to continue after the readable text it takes that enter as an input for read_line again. Which gives it an empty input.
Is that what happens?

Then the question is how to I prevent that? How to make it ask for 3 like it's the first time again?

I assume you were running it in the playground? It works fine if I run it locally with cargo run.

It works correctly on my system as well. What platform and terminal are you using? Is it possible that your terminal is generating two bytes ("\r\n") when you press Enter, and your pause function only consumes one of them?

Really? That's a possibility?
Thanks! You just inspired me to do something great.

How in the blazes did anyone think it was a good idea to make such things so differently that you actually have to write code differently depending on platform or terminal while usig the same language?
That was retorical.
I will not be needing anymore help regarding this issue.
Thanks again mbrubeck!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.