Question:
why do i flush input buffer to read a character?
srinivasa
2010-10-12 19:35:32 UTC
if we read an integer first. input buffer needs to flushed to read a character what is the reason behind it.
Three answers:
Light Cloud
2010-10-12 19:42:27 UTC
Buffers need to be flushed just by the very nature of how buffers work. Buffers are a place to store data, and when the buffer fillls up, the entire stream of stored data is simultaneously dumped out. Computers do this because it's faster.



As an analogy, people don't throw trash out into the big trash bins anytime they have trash. First, we put trash in the trash can; when the trash can fills up, then we take the whole bag and dump it into the bin. We do it this way because it's faster than taking each individual piece of trash out each time. Similarly, when we send (snail) mail to each other, the post office doesn't immediately send out mail each time. It waits for a full day before delivering mail (so it "buffers" mail for 24-hours). Continuing the analogy, waiting for user input is like waiting for mail to arrive. Flushing the buffer is basically forcing the mail carrier to make a trip. Not flushing the buffer means that you will allow the system to decide when to deliver the input to you. Typically, the system will buffer input until the user hits the enter key, and/or the line is so long that the buffer overflows.
JoelKatz
2010-10-13 02:36:52 UTC
It's not clear what your question is about. Is it about specific functions in some specific language? Normally there would be no need to flush the input buffer. What happens if you don't?
Neel
2010-10-13 11:56:07 UTC
its not necessary to flush input buffer aftr read integer for character....

if there is condition read character only then u need to flush..so become input buffer get empty...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...