【发布时间】:2017-05-03 21:23:58
【问题描述】:
我是 C++ 新手,我正在使用 Visual Studio 2015。
cin 在"Please enter another integer:\n" 之后不等待输入,每次都输出"You entered 0"。
我已经在 Internet 上搜索了一个多小时,但没有找到解决方案。 cin.ignore() 的任何组合都不起作用。为什么cin 缓冲区仍未清除?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vals;
int val = 0;
int n = 0;
cout << "Please enter some integers (press a non-numerical key to stop)\n";
while (cin >> val)
vals.push_back(val);
cin.ignore(INT_MAX, '\n');
cin.ignore();
cout << "Please enter another integer:\n";
cin.ignore();
cin >> n;
cout << "You entered " << n;
system("pause");
return 0;
}
【问题讨论】:
标签: c++ input buffer iostream cin