下面这代码按下Ctrl+z结束while输入后,接下来的cin >> val2就无法输入了
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <typeinfo>
using namespace std;
int main()
{
vector<int> vi;
int val;
while (cin >> val)
vi.push_back(val);
int val2;
cout << "Please enter the integers that you want to seek:" << endl;
cin >> val2;
cout << "The sequence contains " << (count(vi.cbegin(), vi.cend(), val2)) << " " << val2 << endl;
return 0;
}
必须在结束while后加一条cin.clear()才能正常运行(注意:要先按回车,再按ctrl+z,因为ctrl+z位于行首才表示结束输入,否则只会被当成一个字符来处理,Ctrl+z的ASCII码是26)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <typeinfo>
using namespace std;
int main()
{
vector<int> vi;
int val;
while (cin >> val)
vi.push_back(val);
int val2;
cin.clear();
cout << "Please enter the integers that you want to seek:" << endl;
cin >> val2;
cout << "The sequence contains " << (count(vi.cbegin(), vi.cend(), val2)) << " " << val2 << endl;
return 0;
}
https://www.cnblogs.com/hubavyn/p/3996413.html节选自此博客