Public Group

clearing the keyboard buffer.

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

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 on other sites
fflush(stdin); clears the input buffer,, to clear the standard output display. try system("cls");

Share on other sites
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 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:

startstart new thread - server threadloop until doneend...server threadoutput = "starting up"...output = "binding"...start new thread sendthreadstart new thread recvthreadloop until doneend of thread...sendthreadoutput = "starting send thread"loop     input into buff[512]untill doneend of thread...start recvthreadoutput = "starting recv thread"loop until done // no code here yetend 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 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
<- text entered here

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

• 10
• 15
• 14
• 18
• 15