How would I clear the istream?

I have a problem where in my program, there is a place that calls the sleep function, and then afterwards it handles user input with _getch(). The problem I have is users pressing a key during Sleep() and it being processed by _getch() promptly after it is done with Sleep(). How can I clear the istream after Sleep()?
Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char c;

    while (true)
    {
        c=_getch();

        //esc to quit
        if (c==27) break;

        cout << c << endl;

        Sleep(1000);

        //clear the buffer
        while (_kbhit()) _getch();
    }

    cout << "hit enter to quit..." << endl;
    cin.get();
    return 0;
}
Thanks! Works perfectly now!
Topic archived. No new replies allowed.