string s;
getline(cin,s);
while (HOW TO WRITE IT HERE?)
{
inputs.push_back(s);
getline(cin,s);
}
views:
190answers:
2
+7
A:
Since I'm too lazy to give a full answer today, I'll just paste what the really useful bot in ##c++ on Freenode has to say:
Using "while (!stream.eof()) {}" is almost certainly wrong. Use the stream's state as the tested value instead: while (std::getline(stream, str)) {}. For further explanation see http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
In other words, your code should be
string s;
while (getline(cin, s))
{
inputs.push_back(s);
}
coppro
2009-08-12 03:21:31
+1. One of the (sadly few) times in C++ where the right choice is straightforward and idiomatic.
j_random_hacker
2009-08-12 03:47:59
A:
string s;
getline(cin,s);
while (!cin.eof)
{
inputs.push_back(s);
getline(cin,s);
}
Tom
2009-08-12 03:23:07
Yes it works and yes it answers the question as stated, but I like coppro's much better I'm afraid. As well as being simpler and more maintainable, coppro's answer will correctly exit if some other I/O error occurs.
j_random_hacker
2009-08-12 03:50:41