How can I read input line(type string) with whitespace? I tried getline but it goes into infinite loop. Following is my code.
#include
#incl
"How can I read input line(type string) with whitespace?"
std::string line;
if (std::getline(std::cin, line)) {
...
}
Note that apart from checking the return value of std:getline
call, you should also avoid mixing >>
operator with std::getline
calls. Once you decide reading the file line by line, it seems to be cleaner and more reasonable to just make one huge loop and do the additional parsing while using string stream object, e.g.:
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) continue;
std::istringstream is(line);
if (is >> ...) {
...
}
...
}