www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using getchar

reply Andrej Mitrovic <andrej.mitrovich test.com> writes:
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.
Sep 06 2010
next sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
Andrej Mitrovic wrote:

 
 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.

It was Jesse Phillips: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=115546
Sep 06 2010
parent Andrej Mitrovic <andrej.mitrovich test.com> writes:
Thanks.

Stanislav Blinov Wrote:

 Andrej Mitrovic wrote:
 
 
 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.

It was Jesse Phillips: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=115546

Sep 06 2010
prev sibling next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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()); } }
Sep 09 2010
next sibling parent Andrej Mitrovic <none none.none> writes:
Jesse Phillips Wrote:

 Hello,
 
 I didn't get much feedback on what was thought about it. I think I'll try 
 the Phobos mailing list... 

Okay, give it a try. :)
 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());
     }
 }

Something like that, but not using readln() since it returns an array of chars and we need a single char.
Sep 09 2010
prev sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
On Thu, 09 Sep 2010 21:44:43 -0400, Andrej Mitrovic wrote:

 Jesse Phillips Wrote:
 
 Hello,
 
 I didn't get much feedback on what was thought about it. I think I'll
 try the Phobos mailing list...

Okay, give it a try. :)
 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());
     }
 }

Something like that, but not using readln() since it returns an array of chars and we need a single char.

I hadn't posted the code yet because it wasn't really general enough. But the example I gave, because readln() should return "a\n" and to!char (...), should convert that into a char just as you want.
Sep 10 2010
prev sibling parent reply Rory McGuire <rjmcguire gmail.com> writes:
Its not skipping its looping on "a\r\n" if you're on windows.

Linux it does the same but only "a\n".

Not sure how you'd make it so that you don't have to wait for the 
return press. Probably has something to do with console settings,
which are probably platform dependent.

-Rory


Andrej Mitrovic wrote:

 I have some D1 code that I'm transfering to D2, and it's using 

 think I need to flush the buffer or something because the loop tends 

 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 

 something like getInput!char(variable), or similar. But I can't find 

 topic, anyone know the link perhaps? It was fairly recent that 

 posted it.

Sep 10 2010
parent Andrej Mitrovic <test test.test> writes:
Yeah, there's a different way of waiting for an actual key press. I've done it
in Python once. But this code was from a dsource tutorial, I didn't write it. :)
I'll find a way to do it properly.

Rory McGuire Wrote:

 Not sure how you'd make it so that you don't have to wait for the 
 return press. Probably has something to do with console settings,
 which are probably platform dependent.

Sep 10 2010