1

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.

Xiao ZHANG
  • 35
  • 5
  • 1
    why don't you look at what `cin.get()` returns? – 463035818_is_not_an_ai Jul 25 '22 at 06:58
  • 1
    some nitpicking: You do not store a string in a `char*`, you store a string in an array of `char` and a `char*` can point to that. Confusing that leads to bugs. – 463035818_is_not_an_ai Jul 25 '22 at 06:59
  • 3
    Why are you using `cin.get()` and character arrays, rather than `std::getline` and `std::string`? – Some programmer dude Jul 25 '22 at 07:00
  • Also, there's not really a "queue" in the computer-science sense, but rather a *buffer*. Basically an array of characters. And the `Enter` key is added as a newline `'\n'` character in that buffer. There's no string terminator in the stream buffer, but `cin.get()` will put the string terminator into your array. – Some programmer dude Jul 25 '22 at 07:03
  • I am using cin.get() cause I am looking C++ primer plus and test all the methods mentioned from beginning. I know string class and getline are more common to use. – Xiao ZHANG Jul 25 '22 at 07:13
  • C++ Primer Plus by Stephen Prata [might not be the best book](https://accu.org/bookreviews/2002/glassborow_1744/). [Here's a list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) of good books. – Some programmer dude Jul 25 '22 at 07:49
  • Thanks sincerely for your help good sir. – Xiao ZHANG Jul 25 '22 at 08:22

1 Answers1

1

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.

john
  • 85,011
  • 4
  • 57
  • 81