varying number of input values

I need to accept input like:
22 56 43 11 34 6 8 7
and put it into a vector, but I don't know how many values there may be
I need to do something like:

cin >> a (until a space)
vector.push_back(a)
repeat until a newline

how would I do this?
The users could be prompted to enter eof (control + Z on Windows, control + D on UNIX) when they're done. That would cause a while(std::cin >> a) loop to break.
Last edited on
Well, if you mean all the values are inputted at once (in one go), then it's relatively easy. Catch the values as a string and then convert them to numbers until the end of the string.
The problem is that the numbers are read from stdin(keyboard by default). when I run the executable, I need to be able to redirect stdin to a file, so it needs to work with keyboard input or from a file. I just need to keep reading integers until I hit a newline.
Then you can do what wasabi suggested. You could use std::stringstream to parse the numbers out of the full string.
Topic archived. No new replies allowed.