简体   繁体   中英

C++ Clear an Input Buffer

As of now, I'm making a program with different screens that use the escape key to exit, but what happens is if I press escape in, lets say the option menu. And then I go into the game, which the game allows escape to exit out, it will automatically leave the game. If that makes sense. :), but, it seems like the escape key floats around in the input buffer, and I tried many ways to clear it but I can't find a way. Here is a part of my code.

int Controls()
{
    // Allows us to get a key when pressed
    int Key;
    Key = _getch();

    switch(Key)
    {
    // Number 27
    case KEY_ESCAPE:
        do code...
        break;

    }
    return Key;
}

Try doing a

fflush(stdin);

whenever you transition to any new page. It clears the input stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM