I can’t get into "EOF"

Asked

Viewed 291 times

0

I’m trying to solve the following URI problem inserir a descrição da imagem aqui

the problem is time to make the EOF happen, I understand more or less how it works but still can not make my program "stop".

int main(){
string entrada;
Arv_bin * arv = new Arv_bin;
getline(cin, entrada);
do{
    entrada = remove_espacos(entrada);
    arv->raiz = cria_nodo(entrada, 0, entrada.length()-1);
    imprime_arv(arv);
    getline(cin, entrada);
    if(cin.eof()) cout << endl;
}while(!cin.eof());
return 0;}

Is there something wrong with my condition? What should I do?

  • Note that if you give EOF at first reading you will treat it anyway. Change the do { ...} while by a while(!cin.eof()) {.... This is unnecessary if(cin.eof()) cout << endl; just put cout << endl; after the loop.

1 answer

0

In the URI (Online Judge), EOF (end-of-file) refers to the end of the file that will be used in the test to verify the code. In this case, we have different ways to read the input (istream/iostream) until the end of the file (eof). How:

while(getline(cin, entrada)){}

In C++ it occurs that the Cin return false when you have an error or reached the end of the file, and true otherwise. While not reaching the end of the file the while will be in true condition and running. When you arrive at the end of the file, Cin will return false and its repeat structure will be finished.

Studies on streambuf, istream and ios_base::iostate flags clarified on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.