C++ 我如何冲洗cin缓冲区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:05:20  来源:igfitidea点击:

How do I flush the cin buffer?

c++cinio-buffering

提问by Martin York

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

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

采纳答案by Evan Teran

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).

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

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

另外:您可能还想做一个:std::cin.clear();在此之前重置流状态。

回答by Martin York

I would prefer the C++ size constraints over the C versions:

与 C 版本相比,我更喜欢 C++ 大小限制:

// Ignore to the end of file
cin.ignore(std::numeric_limits<std::streamsize>::max())

// Ignore to the end of line
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

回答by jyggorath

cin.clear();
fflush(stdin);

This was the only thing that worked for me when reading from console. In every other case it would either read indefinitely due to lack of \n, or something would remain in the buffer.

这是从控制台读取时唯一对我有用的东西。在其他所有情况下,它要么由于缺少 \n 而无限期地读取,要么将某些内容保留在缓冲区中。

EDIT: I found out that the previous solution made things worse. THIS one however, works:

编辑:我发现之前的解决方案让事情变得更糟。然而,这个有效:

cin.getline(temp, STRLEN);
if (cin.fail()) {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

回答by Ben

I have found two solutions to this.

我找到了两个解决方案。

The first, and simplest, is to use std::getline()for example:

第一个,也是最简单的,是使用std::getline()例如:

std::getline(std::cin, yourString);

... that will discard the input stream when it gets to a new-line. Read more about this function here.

...当它到达换行符时将丢弃输入流。在此处阅读有关此功能的更多信息。

Another option that directly discards the stream is this...

另一个直接丢弃流的选项是这个......

#include <limits>
// Possibly some other code here
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Good luck!

祝你好运!

回答by Ben

int i;
  cout << "Please enter an integer value: ";

  // cin >> i; leaves '\n' among possible other junk in the buffer. 
  // '\n' also happens to be the default delim character for getline() below.
  cin >> i; 
  if (cin.fail()) 
  {
    cout << "\ncin failed - substituting: i=1;\n\n";
    i = 1;
  }
  cin.clear(); cin.ignore(INT_MAX,'\n'); 

  cout << "The value you entered is: " << i << " and its double is " << i*2 << ".\n\n";

  string myString;
  cout << "What's your full name? (spaces inclded) \n";
  getline (cin, myString);
  cout << "\nHello '" << myString << "'.\n\n\n";

回答by Ben

How about:

怎么样:

cin.ignore(cin.rdbuf()->in_avail());

回答by Shadow2531

I prefer:

我更喜欢:

cin.clear();
fflush(stdin);

There's an example where cin.ignore just doesn't cut it, but I can't think of it at the moment. It was a while ago when I needed to use it (with Mingw).

有一个例子,cin.ignore 只是没有削减它,但我现在想不起来。不久前我需要使用它(与 Mingw 一起使用)。

However, fflush(stdin) is undefined behavior according to the standard. fflush() is only meant for output streams. fflush(stdin) only seems to work as expected on Windows (with GCC and MS compilers at least) as an extension to the C standard.

但是,根据标准, fflush(stdin) 是未定义的行为。fflush() 仅适用于输出流。fflush(stdin) 似乎只能在 Windows 上按预期工作(至少使用 GCC 和 MS 编译器)作为 C 标准的扩展

So, if you use it, your code isn't going to be portable.

所以,如果你使用它,你的代码就不会是可移植的。

See Using fflush(stdin).

请参阅使用 fflush(stdin)

Also, see http://ubuntuforums.org/showpost.php?s=9129c7bd6e5c8fd67eb332126b59b54c&p=452568&postcount=1for an alternative.

另请参阅http://ubuntuforums.org/showpost.php?s=9129c7bd6e5c8fd67eb332126b59b54c&p=452568&postcount=1以获取替代方案。

回答by Bobul Mentol

Another possible (manual) solution is

另一种可能的(手动)解决方案是

cin.clear();
while (cin.get() != '\n') 
{
    continue;
}

I cannot use fflush or cin.flush() with CLion so this came handy.

我不能在 CLion 中使用 fflush 或 cin.flush() 所以这很方便。

回答by Chaitanya Vaishampayan

Easiest way:

最简单的方法:

cin.seekg(0,ios::end);
cin.clear();

It just positions the cin pointer at the end of the stdin stream and cin.clear() clears all error flags such as the EOF flag.

它只是将 cin 指针定位在 stdin 流的末尾,cin.clear() 清除所有错误标志,例如 EOF 标志。

回答by Gunnar Steinn

The following should work:

以下应该工作:

cin.flush();

On some systems it's not available and then you can use:

在某些系统上它不可用,然后您可以使用:

cin.ignore(INT_MAX);