goktug96 goktug96 - 2 months ago 9
C++ Question

Task of getline

getline
function can takes three parameters. First of them is the object
cin
. Second of them is the object defined with the class
string
. Third of them is delimiter character.

1) If the delimiter character is not found, what happens?

2) For example, I chose character
't'
for delimiter.Then, the user entered
"Jonathan"
. Does
"han"
remain in buffer or
"han\n"
remain in buffer? (I read that
getline
deletes newline in buffer in every time although delimiter is not newline character but I do not ensure this.)

Answer

This is documented in std::getline

2) Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.

In the case of std::getline(std::cin, str, 't');, this means str will contain Jona, t will be extracted and discarded, keeping han\n in the input buffer.

Comments