You might want something like this:
int inp;
while(cin >> inp){
....
if(cin.peek() == '\n')
cin.clear(ios::eofbit);
....
}
The while loop runs as long as the I/O is successful. Assuming you want to end input when the line of integers ends, you set the eofbit
manually when a \n
is encountered. This is checked with the condition if(cin.peek() == '\n')
. When the condition is true the while loop terminates. In the example below, I demonstrate how to read a line of integers separated by space into a vector and then print it separated by space
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;
int main(){
vector<int> coll;
int inp;
while(cin >> inp){
if(cin.peek() == '\n')
cin.clear(ios::eofbit);
coll.push_back(inp);
}
copy(
coll.cbegin(),
coll.cend(),
ostream_iterator<int>(cout, " ")
);
cout << endl;
}
cin.eof()
– Cytogenesis