I'm grabbing input from a standard input stream. Such as,
1 2 3 4 5
or
1
2
3
4
5
I'm using:
std::string in;
std::getline(std::cin, in);
But that just grabs upto the newline, correct? How can I get input whether they are separated by newline OR whitespace(s) using only iosteam, string, and cstdlib?
Use
'q'
as the the optional argument togetline
.http://ideone.com/I2vWl
Just use:
operator>>
will skip whitespace by default. You can chain things to read several variables at once:getline()
reads everything on a single line, returning that whether it's empty or contains dozens of space-separated elements. If you provide the optional alternative delimiter alagetline(std::cin, my_string, ' ')
it still won't do what you seem to want, e.g. tabs will be read intomy_string
.Probably not needed for this, but a fairly common requirement that you may be interested in sometime soon is to read a single newline-delimited line, then split it into components...
the user pressing enter or spaces is the same.
This would read from standard input if it space separated or line separated .
std::getline( stream, where to?, delimiter ie
or you can read in a line, then tokenize it based on whichever delimiter you wish.