Two questions about input

My first question relates to the endl manipulator, which writes a newline character to the output and flushes the buffer. What does flushing the buffer mean?

My second question relates to streams in general. Sometimes, I see code like this:

1
2
3
4
5
char c;
while (cin.get(c))
{
// code
}


How does cin.get(c) know to get the next character? Are there flags set after get() is called, so it knows where to get the next character next time it is called? Is this how all streams work?
Last edited on
When you send output to std::cout, the data isn't necessarily immediately sent to the device. It can be written to an intermediate buffer until enough output has been collected. This is because it's faster to output n bytes in one go than to output 1 byte n times.
"Flushing the buffer" means to definitely write the entire buffer to the device.

How does cin.get(c) know to get the next character? Are there flags set after get() is called, so it knows where to get the next character next time it is called?
I'm not sure what you're asking. Can you give an example?
@helios,

Thanks for the response.

When you send output to std::cout, the data isn't necessarily immediately sent to the device. It can be written to an intermediate buffer until enough output has been collected. This is because it's faster to output n bytes in one go than to output 1 byte n times.
"Flushing the buffer" means to definitely write the entire buffer to the device.

What is an example of an immediate buffer when using the endl manipulator?

I'm not sure what you're asking. Can you give an example?

As for an example, lets say I have this code:

1
2
3
4
5
char c;
while (cin.get(c))
{
// code
}


If somebody inputs lets say "Hello". cin.get(c) will get the stuff in the stream ("Hello") and put it in c. What happens next is what I am asking. Does C++ put a flag at the end of "Hello", so that next time cin.get(c) is called (lets say it is called with " world!") it doesn't print "Hello world!" but instead " world!"? Or is just the data in the stream cleared?
Last edited on
What is an example of an immediate buffer when using the endl manipulator?
Again, I don't know what it is you're asking.

If somebody inputs lets say "Hello". cin.get(c) will get the stuff in the stream ("Hello") and put it in c. What happens next is what I am asking. Does C++ put a flag at the end of "Hello", so that next time cin.get(c) is called (lets say it is called with " world!") it doesn't print "Hello world!" but instead " world!"? Or is just the data in the stream cleared?
If the user inputs "Hello", the input gets pushed into a queue-like (FIFO) buffer. Each call to get() returns the next character in the buffer until the buffer is empty, and the user isn't prompted at all. Once the buffer is empty, the user is prompted again for more input.
Okay, I understand now. So if there is no endl, then it just outputs each character one by one?

Compare this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
  std::cout << "Output" << std::endl;
  
  
  for (int i=0; i<999999999; ++i)
    {
    }
}


with this.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
  std::cout << "Output";
  
  
  for (int i=0; i<999999999; ++i)
    {
    }
}


Build them. Run them. On my system, the first one outputs the word to screen and then goes through the loop. The second goes through the loop and then the word appears on screen.

When you use cout, the words go into a buffer. They may not go straight to the screen. They may go to the screen at a later time. While they wait to go to screen, they go somewhere else. That somewhere else is a buffer.

You can force the letters to go to screen by doing various things. One such thing is endl, which (in addition to causing the output to move to the next line) flushes the buffer; this means that wherever the letters go before going onto the screen (i.e. the buffer) is emptied - everything in it is processed and goes to screen.


Last edited on
Okay, I understand, thanks.
Topic archived. No new replies allowed.