I'm new to C++ and I'm using Visual Studio 2015.
cin
is not waiting for input after "Please enter another integer:\n"
and outputs "You entered 0"
every time.
I've searched the Internet more than an hour without a solution. No combination of cin.ignore()
is working. Why is the cin
buffer still not cleared?
#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;
}