stdout - autoflushing

Benji romanbeno273 at gmail.com
Tue Dec 3 10:10:37 PST 2013


On Tuesday, 3 December 2013 at 17:49:32 UTC, H. S. Teoh wrote:
> On Tue, Dec 03, 2013 at 06:12:20PM +0100, Benji wrote:
>> Hello,
>> in order to have correctly displayed output (before reading
>> something from stdin),
>> I must call stdout.flush().
>> Sometimes, it's really annoying, especially when it is 
>> necessarry to
>> call it 10 times.
>> 
>> For example:
>> write("Enter some string: ");
>> stdout.flush();
>> string a = readln();
>> write("And again please: ");
>> stdout.flush();
>> string b = readln();
>> ...
>> 
>> Is there any way to prevent this?
>
> What about:
>
> 	void prompt(A...)(string fmt, A args)
> 	{
> 		writef(fmt, args);
> 		stdout.flush();
> 		return readln();
> 	}
>
> 	auto a = prompt("Enter your name: ");
> 	auto b = prompt("Enter your age: ").to!int;
> 	... // etc.
>
>
> T

Thanks, I didn't think about that (I'm beginner)


More information about the Digitalmars-d-learn mailing list