Reputation: 1480
I have some code and I wanted to use cin.eof() to stop my program from reading the input. I was thinking of doing:
char array[10]
while(!cin.eof())
{
for(int i = 0; i < 10; i++)
{
cin >> array[i];
}
}
And the code goes on. However, when I click '\n', my output is outputted. When i click cntrl + d, (on a UNIX terminal) the program performs the output again and then proceeds to end. How do I get it so that my program stops reading at a newline and prints my output when I type cntrl + d only once?
Thanks.
Upvotes: 1
Views: 8336
Reputation: 153909
First, cin.eof()
doesn't do anything useful until input has failed.
You never want to use it at the top of a loop.
Secondly, it's not really clear to me what you are trying to do. Something like:
std::string line;
while ( std::getline( std::cin, line ) ) {
// ...
}
perhaps? This will read a line of text into the variable line
, until
end of file; when you encounter an end of file, the input will fail, and
you will leave the loop.
Upvotes: 1