clearing the input buffer - z88

Discussion about other targets
Post Reply
Jbizzel
Member
Posts: 14
Joined: Tue Jun 29, 2021 7:20 pm

clearing the input buffer - z88

Post by Jbizzel »

Hi,

I'm using a timer like this:

Code: Select all

void timer() {
   int i;
   printf("          3 Minute Timer:");

for( i = 1 ; i < 11 ; i++ ){
   sleep(18);
   printf("%d,", i );
}
   return(0);
}
And the user can press a key to move the timer on more quickly - which I'm happy with that happening.

However, the next time I use something like scanf:

Code: Select all

scanf("%d", &roundScore1);
it dumps all previous key presses into the input. I assume I have to flush the input buffer, so I tried:

Code: Select all

fflush(stdin);
but that hasn't helped. Please can anyone help regarding this problem?

J
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: clearing the input buffer - z88

Post by dom »

Interesting problem - never seen that before.

At the moment, fflush() is pretty much a noop - it returns -1 to indicate that nothing was done. Hooking into os_pur should be possible. But until I do that, just call getk() after the sleep - that should consume the keypress.
Jbizzel
Member
Posts: 14
Joined: Tue Jun 29, 2021 7:20 pm

Re: clearing the input buffer - z88

Post by Jbizzel »

thanks Dom, that works very well! :)
Post Reply