how stdin stream works?

Flade consolemaster0 at gmail.com
Wed Aug 19 18:46:55 UTC 2020


On Wednesday, 19 August 2020 at 18:11:23 UTC, Steven 
Schveighoffer wrote:
> 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

Thanks Steve! I will get the input a string then as you said and 
then I'll try to convert it! Thanks a lot, have a nice day!


More information about the Digitalmars-d-learn mailing list