It is well known that say if we use a loop of
cin.get(char_array, 10)
to read keyboard input. It will leave a 'delimiter'(corresponds to the 'ENTER' key) in the queue so the next iteration of cin.get(char_array, 10) will read the 'delimiter' and suddenly closed. We have to do this
cin.get(char_array, 10)
cin.get()
use an additional cin.get() to absorb the 'delimiter' so that we can input several lines in prompt in a row.
I really wonder here is, what exactly is the delimiter in the queue? Is it just '\0' which marks the end of a string? Say if I type "Hello" in prompt and push ENTER button, will the queue be like {'H','e','l','l','o','w','\0'}?
If the delimiter(corresponds to the ending "ENTER" key) is not '\0', will there be a '\0' automatically added to the end of the queue? say {'H','e','l','l','o','w','whatever_delimiter','\0'}
This question matters cause if we use a char* to store the input, we may consider extra places for those special marks. The actual chars you can store is fewer than you the seats you applied.
CodePudding user response:
The 'queue' associated with std::cin
(it's normally known as a buffer) is the characters you type. This is true however you read from std::cin
.
get(array, count, delim)
reads until 1) end of file is reached, 2) count - 1
characters have been read, or 3) the next character in the queue is delim
. If not supplied delim
defaults to '\n'
.
So in your case the delim is '\n'
and the read will continue until '\n'
is the next character in the queue (assuming the read stops for reason 3 above).
A '\0'
is added to array
(assuming there is room) by the get
function. It's never the case that '\0'
is added to the queue.