我如何冲洗cin缓冲区? [英] How do I flush the cin buffer?

查看:32
本文介绍了我如何冲洗cin缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何清除 C++ 中的 cin 缓冲区?

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

推荐答案

可能:

std::cin.ignore(INT_MAX);

这会读入并忽略所有内容,直到 EOF.(您还可以提供第二个参数,它是要读取的字符,直到(例如:' ' 忽略单行).

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

此外:您可能还想在此之前执行:std::cin.clear(); 以重置流状态.

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

这篇关于我如何冲洗cin缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆