I was asked a question to write a function which takes and returns an istream&
. The function should read the stream until it hits EOF
and the value read should be printed with standard output.
Here is my code which works nearly fine:
#include<iostream>
#include<string>
#include<istream>
std::istream& fun(std::istream &ob)
{
std::string s;
while((ob>>s)&&(!ob.eof()))
std::cout<<s<<'\n';
return ob;
}
int main()
{
std::istream &obj = fun(std::cin);
return 0;
}
The only part which is not happening is program (istream
) reaching to EOF
- I have to manually enter ctrl + d
on the terminal to stop the loop - is there any other way around to do this?