Hi everyone. My code keeps skipping past the cin statement after my loop. I'm trying to let the user fill in a vector, and the user enters a character when they're done entering integers. Somehow filling in that vector is causing the code to skip past any future cin statements. I've tried fixing this by adding cin.ignore() statements and such, but nothing seems to work.
1 2 3 4 5 6 7 8 9 10
int x,n;
vector<int> list;
while(cin>>x){
list.push_back(x);
}
cin>>n;
You need both cin.ignore() and cin.clear().
When the user enters a character, cin's fail bit is set and will stay set until you clear it.
cin.clear() will clear the fail bit. cin.ignore() will remove the character which is still in the input buffer.
Since the only way to exit the while loop is to put the stream in an error condition you will need to clear() the error flags and remove the offending characters. Something like: