Using getchar

Jesse Phillips jessekphillips+D at gmail.com
Thu Sep 9 18:30:35 PDT 2010


On Mon, 06 Sep 2010 17:42:05 -0400, Andrej Mitrovic wrote:

> I have some D1 code that I'm transfering to D2, and it's using getchar.
> I think I need to flush the buffer or something because the loop tends
> to skip:
> 
> import std.c.stdio;
> import std.stdio;
> 
> void main()
> {
>     char k;
> 
>     for(int i = 0; i < 10; i++)
>     {
>         writef("Press key #%d:\t\n", i);
>         k = cast(char)getchar();
>     }
> }
> 
> E.g.:
> Press key #0:
> a
> Press key #1:
> Press key #2:
> b
> Press key #3:
> Press key #4:
> c
> Press key #5:
> Press key #6:
> 
> I guess I could use scanf() instead.. or maybe something more D-ish
> perhaps? :)
> 
> Someone on the NGs started creating some user-friendly input functions,
> something like getInput!char(variable), or similar. But I can't find the
> topic, anyone know the link perhaps? It was fairly recent that someone
> posted it.

Hello,

I didn't get much feedback on what was thought about it. I think I'll try 
the Phobos mailing list... without my library the code would look 
something like (sorry cant test right now)

import std.stdio;

void main()
{
    char k;

    for(int i = 0; i < 10; i++)
    {
        writef("Press key #%d:\t\n", i);
        k = std.conv.to!char(readln());
    }
}


More information about the Digitalmars-d-learn mailing list