www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - flush input buffer?

reply Michael P. <baseball.mjp gmail.com> writes:
Okay, so I'm sure if flushing the input buffer is what I want, or if that even
makes sense, so I'll say what I'm trying to do first to see if that's what I
want.
So, I made this program where you specify the number of digits in a number you
want to remember. Then, it ask you how many questions.
Anyways, a number will show up. Then, you have 4 seconds to remember. Then, 30
lines are skipped as my "clearing of the screen". Sleep another second.
You are then asked to type in the number you saw.
The problem I have is that the user can just type in the number while they see
it, and what the typed will show up when the prompt comes up.
What I want to do is "flush the input buffer" just before I request for input
so the user can no longer type in the number while they see it.

-Michael P.
Oct 18 2008
parent reply "Nick Sabalausky" <a a.a> writes:
"Michael P." <baseball.mjp gmail.com> wrote in message 
news:gdd02h$1uui$1 digitalmars.com...
 Okay, so I'm sure if flushing the input buffer is what I want, or if that 
 even makes sense, so I'll say what I'm trying to do first to see if that's 
 what I want.
 So, I made this program where you specify the number of digits in a number 
 you want to remember. Then, it ask you how many questions.
 Anyways, a number will show up. Then, you have 4 seconds to remember. 
 Then, 30 lines are skipped as my "clearing of the screen". Sleep another 
 second.
 You are then asked to type in the number you saw.
 The problem I have is that the user can just type in the number while they 
 see it, and what the typed will show up when the prompt comes up.
 What I want to do is "flush the input buffer" just before I request for 
 input so the user can no longer type in the number while they see it.

 -Michael P.

I'm not sure what lib or function you're using to read the input, but there should be some way to do an asynchronous read from the input buffer to read input *only* if input already exists and just sinply return nothing instead of waiting if no input exists. Then you could do something like this: do { inputDataToThrowAway = AsyncRead(); } while(data was actualy read); output("Please enter your answer"); waitForInputAndReadIt();
Oct 18 2008
parent reply Michael P. <baseball.mjp gmail.com> writes:
Nick Sabalausky Wrote:

 "Michael P." <baseball.mjp gmail.com> wrote in message 
 news:gdd02h$1uui$1 digitalmars.com...
 Okay, so I'm sure if flushing the input buffer is what I want, or if that 
 even makes sense, so I'll say what I'm trying to do first to see if that's 
 what I want.
 So, I made this program where you specify the number of digits in a number 
 you want to remember. Then, it ask you how many questions.
 Anyways, a number will show up. Then, you have 4 seconds to remember. 
 Then, 30 lines are skipped as my "clearing of the screen". Sleep another 
 second.
 You are then asked to type in the number you saw.
 The problem I have is that the user can just type in the number while they 
 see it, and what the typed will show up when the prompt comes up.
 What I want to do is "flush the input buffer" just before I request for 
 input so the user can no longer type in the number while they see it.

 -Michael P.

I'm not sure what lib or function you're using to read the input, but there should be some way to do an asynchronous read from the input buffer to read input *only* if input already exists and just sinply return nothing instead of waiting if no input exists. Then you could do something like this: do { inputDataToThrowAway = AsyncRead(); } while(data was actualy read); output("Please enter your answer"); waitForInputAndReadIt();

Oh yeah, forgot to say that I am using din.readf() for input.
Oct 18 2008
parent reply "Nick Sabalausky" <a a.a> writes:
"Michael P." <baseball.mjp gmail.com> wrote in message 
news:gddeog$26je$1 digitalmars.com...
 Nick Sabalausky Wrote:

 "Michael P." <baseball.mjp gmail.com> wrote in message
 news:gdd02h$1uui$1 digitalmars.com...
 Okay, so I'm sure if flushing the input buffer is what I want, or if 
 that
 even makes sense, so I'll say what I'm trying to do first to see if 
 that's
 what I want.
 So, I made this program where you specify the number of digits in a 
 number
 you want to remember. Then, it ask you how many questions.
 Anyways, a number will show up. Then, you have 4 seconds to remember.
 Then, 30 lines are skipped as my "clearing of the screen". Sleep 
 another
 second.
 You are then asked to type in the number you saw.
 The problem I have is that the user can just type in the number while 
 they
 see it, and what the typed will show up when the prompt comes up.
 What I want to do is "flush the input buffer" just before I request for
 input so the user can no longer type in the number while they see it.

 -Michael P.

I'm not sure what lib or function you're using to read the input, but there should be some way to do an asynchronous read from the input buffer to read input *only* if input already exists and just sinply return nothing instead of waiting if no input exists. Then you could do something like this: do { inputDataToThrowAway = AsyncRead(); } while(data was actualy read); output("Please enter your answer"); waitForInputAndReadIt();

Oh yeah, forgot to say that I am using din.readf() for input.

Ok, I took a look at the phobos documentation for that. Looks like this should work (I use Tango instead of phobos, so I haven't tested it. Hopefully it'll work): void clearInputBuffer() { while(din.available() > 0) { din.getc(); } } Calling that function before you read the user's input *should* do what you're looking for.
Oct 18 2008
parent Michael P. <baseball.mjp gmail.com> writes:
Nick Sabalausky Wrote:

 "Michael P." <baseball.mjp gmail.com> wrote in message 
 news:gddeog$26je$1 digitalmars.com...
 Nick Sabalausky Wrote:

 "Michael P." <baseball.mjp gmail.com> wrote in message
 news:gdd02h$1uui$1 digitalmars.com...
 Okay, so I'm sure if flushing the input buffer is what I want, or if 
 that
 even makes sense, so I'll say what I'm trying to do first to see if 
 that's
 what I want.
 So, I made this program where you specify the number of digits in a 
 number
 you want to remember. Then, it ask you how many questions.
 Anyways, a number will show up. Then, you have 4 seconds to remember.
 Then, 30 lines are skipped as my "clearing of the screen". Sleep 
 another
 second.
 You are then asked to type in the number you saw.
 The problem I have is that the user can just type in the number while 
 they
 see it, and what the typed will show up when the prompt comes up.
 What I want to do is "flush the input buffer" just before I request for
 input so the user can no longer type in the number while they see it.

 -Michael P.

I'm not sure what lib or function you're using to read the input, but there should be some way to do an asynchronous read from the input buffer to read input *only* if input already exists and just sinply return nothing instead of waiting if no input exists. Then you could do something like this: do { inputDataToThrowAway = AsyncRead(); } while(data was actualy read); output("Please enter your answer"); waitForInputAndReadIt();

Oh yeah, forgot to say that I am using din.readf() for input.

Ok, I took a look at the phobos documentation for that. Looks like this should work (I use Tango instead of phobos, so I haven't tested it. Hopefully it'll work): void clearInputBuffer() { while(din.available() > 0) { din.getc(); } } Calling that function before you read the user's input *should* do what you're looking for.

No, unfortunately that didn't work. I tried calling din.flush() before the input too and it didn't work either.
Oct 18 2008