I am wondering if there is any way to detect the end of a user's input in a console app without having to use a sentinel character. Will someone please help me?
I am wondering if there is any way to detect the end of a user's input in a console app without having to use a sentinel character. Will someone please help me?
Well in C, its
I imagine that the cin.getline() or whatever functions return some useful status to indicate end of file.Code:while ( fgets( buff, sizeof buff, stdin ) != NULL ) { // do stuff }
You normally signal EOF on stdin by typing either ctrl-z or ctrl-d (depending on your OS)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>I imagine that the cin.getline() or whatever functions return some useful status to indicate end of file.
Yep, istreams have an implicit conversion that allows simple status checking:
Code:while ( cin.getline ( buff, sizeof buff ) ) { // Do stuff }
My best code is written with the delete key.
Perhaps he wants to know if he read the whole line, inwhich case he needs to check to make sure the last character is '\n'. If you use a loop it will keep on begging for input until they give EOF (or an error occures) which is usually not what someone desires.