how stdin stream works?

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 19 18:11:23 UTC 2020


On 8/19/20 1:44 PM, Flade wrote:
> Hi everyone! I'm trying to do error handling (with the try block) and 
> when I give a wrong value to the variable (it is an integer and I give a 
> non-number value), then It doesn't let me re get input. The code:
> 
> 
> int x;
> 
> bool not_accepted = false;
> 
> while (!not_accepted) {
> 
>      try {
> 
>          write("x: ");
> 
>          readf("%d\n", x);
> 
>          not_accepted = true;
> 
> } catch (Exception msg) {
> 
>          writeln("Please give a right coordinate");
> 
>      }

Probably readf stops as soon as it encounters an error, meaning that 
your input is still present.

Try instead getting a line via readln, and then trying to read that into 
your expected input.

-Steve


More information about the Digitalmars-d-learn mailing list