varying number of input values

Aug 29, 2010 at 3:11am
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?
Aug 29, 2010 at 3:16am
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 Aug 29, 2010 at 3:16am
Aug 29, 2010 at 3:18am
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.
Aug 29, 2010 at 3:23am
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.
Aug 29, 2010 at 3:26am
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.