'Get all the user input with EOF?
I'm having problems getting all the user input from the terminal. It's always one line short, and it's the last line.
do{
getline(std::cin, input);
buffer += input;
buffer += '\n';
}while(!input.empty());
gives me the desired output, but it ends when I press Enter, and I want it to end when it reaches End Of File(EOF), when i press Ctrl+D. I have tried
while(!(std::cin >> input).eof()){
buffer += input;
buffer += '\n';
}
but it doesn't get the last line. Adding the input to the buffer after the while-loop also doesn't solve it.
Solution 1:[1]
Keep reading input with getline:
std::string buffer;
std::string input;
while (std::getline(std::cin, input))
{
buffer += input;
buffer += '\n';
}
When you input EOF getline will set eofbit and the loop will end.
In any case the second snippet should store the last input as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
