-
How to deal with EOF ?
I thought it would be simple.. but couldn't come up with a solution.
How would I stop the following program from entering an Infinite loop when it gets an eof ?
Code:
#include<iostream>
#include<string>
int main()
{
std::string str;
while(true)
{
std::cout<<"Input: ";
std::getline(std::cin,str);
std::cout<<str<<std::endl;
}
return 0;
}
-
Well one way is to use exceptions:
Code:
cin.exceptions (ios::eofbitif||ios::failbit | ios::badbit)
Another way is a simple break:
Code:
if(!std::getline(std::cin,str)) break;