How do I flush the cin buffer?

前端 未结 13 1061
無奈伤痛
無奈伤痛 2020-11-22 00:14

How do I clear the cin buffer in C++?

13条回答
  •  抹茶落季
    2020-11-22 00:21

    Possibly:

    std::cin.ignore(INT_MAX);
    

    This would read in and ignore everything until EOF. (you can also supply a second argument which is the character to read until (ex: '\n' to ignore a single line).

    Also: You probably want to do a: std::cin.clear(); before this too to reset the stream state.

提交回复
热议问题