I am trying to accomplish the simple task of reading space-separated numbers from the console in vector<int> , but I do not understand how to do this correctly.
This is what I have done so far:
int n = 0; vector<int> steps; while(cin>>n) { steps.push_back(n); }
However, this requires the user to click an invalid character (e.g. a ) to break the while . I do not want it.
As soon as the user enters numbers like 0 2 3 4 5 and presses Enter , I want the loop to be broken. I also tried using istream_iterator and cin.getline , but I could not get it to work.
I donβt know how many elements the user enters, so I use vector .
Please suggest the right way to do this.
Asha
source share