Why is my code not recoginising blank line C++

69 views Asked by At

I had a look at this example, on the istream get method to extract characters from a file stream.

Basically, I don't want my program to read a blank line, however it skips to the next line and gives an error that it can't read the next line - when really it shouldn't read the blank line.

Anyways, this is the tutorial I followed http://www.cplusplus.com/reference/istream/istream/get/ and this is my code

is.get(c)
if(c == '\0')
{
  cout << "Blank line" << endl;
}
else
{
 is.getline(...);
}

ignore whitespace...
is.getline(...);
1

There are 1 answers

0
Dietmar Kühl On

You seem to be under the impression that a blank line is somehow represented by a null character. It is not. A blank line just consists of an end of line character ('\n', possible originally of a line end sequence but on systems where it matters the line end sequence is transfirmed into a line end character in non-std::ios_base::binary mode). To detect an empty line you'd read a line using std::getline() or, possibly, using std::istream::getline() and check if the result is empty.