Hamza Khan Hamza Khan - 2 months ago 28
C++ Question

Does std::getline() empty the buffer completely?

Does std::getline() empties the buffer completely or do we still have the newline character left in the input buffer?


As the page says

If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).

It should be discarded.

Check out this example. The newline in between the two sentences is appropriately discarded.

Answering to comment:

(1) istream& getline (istream& is, string& str, char delim);

(2) istream& getline (istream& is, string& str);

Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).

Yes, newline acts as the default delimiter.

No, the input buffer would not have the newline character in it anymore as it would be succinctly discarded. You don't need to clean by yourself.