• Advertisement
Sign in to follow this  

clearing the keyboard buffer.

This topic is 4800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in a win32 console project, in c++ I have: ...loads of code... char buff[512]; cin.getLine(buff,500); If you press buttons before the cin.getLine, it appears in the console, so I want to clear the buffer before I get to that point, so noting appears on screen. I have tried gets, fflush, rewind and none of them do what I want. sample output: starting up binding awaiting connection connected starting recvthread asdfghjthm starting send thread // note the keys prssed earlier. enter text -> I hope that explains it.

Share this post


Link to post
Share on other sites
Advertisement
In C++, the idiomatic solution is std::cin.ignore(std::numeric_limits<size_t>::max());. You will need the <limits> header.

Note that, for best performance (std::ios::sync_with_stdio(false);), mixing C and C++ IO in a program should be avoided.

Share this post


Link to post
Share on other sites
Thanks for the suggestions, but none of them do what I want them to do.

What I want is something that will stop anything that is typed from appearing on screen, untill something else is called.

here is some pseudo:


start

start new thread - server thread

loop until done

end

...

server thread

output = "starting up"
...
output = "binding"
...

start new thread sendthread
start new thread recvthread

loop until done

end of thread

...

sendthread

output = "starting send thread"
loop

input into buff[512]

untill done
end of thread

...

start recvthread

output = "starting recv thread"

loop until done // no code here yet

end of thread


now If someone presses buttons during the setup, they are outputed as soon as cin.getline is called.

sample out put

starting up
binding
starting send thread
starting re hbhknhjuihi cvthread <- here is where getline is called.


What I need, is that the rubbish pressed before getline is discarded.

i.e.

someline to discard anything previously pressed;
cin.getline(...


output:

starting up
binding
starting send thread
starting recv thread
<- text entered here


i.e. no rubbish een if I press keys.
I hope that explains it all properly this time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement