Question:
explain the purpose of cin.flush in c++?
Zia ur Rahman
2009-10-22 00:59:19 UTC
dear sir please explain this flush function or it is operator i don't know what it is , please explain it and give me example also.
Four answers:
Jim
2009-10-22 01:21:00 UTC
flush is a method of ostream (cout, cerr), istream(cin), ofstream, ifstream.



iostream and stdio.h use buffers when you read and write. it is like a cache. sometimes at critical moments these buffers need to be flushed. that is entirely up to you when you need to cin.flush() or cout.flush() or whatever.flush() on the fstream.



for example, if you are doing shared file writes, I would suggest using Win32 OS calls, as there is no guarantee that iostreams will allow shared writes or reads on your file while is is being written. but that is a concurrent access problem. you can try iostream and see what happens with shared writes and reads.



for instance, you may want to make a block of writes are finished before allowing a second program to access your file, even though it is still open. allow the 2nd program to finish its reads, then write your 2nd block of writes. This is not the usual case. but it might be the case for a database. you would probably need semaphores to control locking of the file or mutexes or something like that.
jplatt39
2009-10-22 01:40:41 UTC
Computers accept input into a buffer. This means the keystroke doesn't go directly into the OS but into a block of memory where it can wait for the OS and applications programs to process it.



Now many functions, like cin or cstdio's scanf don't accept input until you hit the return key. This is not an issue for a string or char array (the ASCIIZ string) but it can be an issue for anything else. To take an example:



You have a menu program to convert between celsius and fahrenheit:



cout << "Enter 1 to convert from Fahrenheit to Celsius" << endl;

cout << "Enter 2 to convert from Celsius to Fahrenheit" << endl;

cout << "Your choice:"

cin >> char choice;

cout << "Enter a temperature to convert:";

cin >> float temp;



It accepts your choice but skips past accepting input for your variable to work on.



That is because the function demanded a letter and an enter key for choice, but only used the letter '1' or '2' and left the enter key in the buffer. The next call to cin read the enter key -- and that was that. It assumed, because it does, that you had entered temp and it should use the value it had (either 0 or some garbage left over from an earlier program).



cin.flush() clears the input buffer. It allows you to enter a value right after entering another value. By the way, the basic flush() is a member of ostreams like cout not istreams like cin in standard C++ but compilers will enable it.
koppe74
2009-10-22 01:21:26 UTC
flush() is a member-function of the fstream-class... in this case used on cin, which is an instance of (if I remember correctly) the iostream-class (a class derived from fstream).



It's purpose is to flush the buffer of a file/stream.



cin is typically your keyboard, so all keystrokes goes here.



You use cin.flush() after gathering some input from the user, to assure that the buffer is emptied before you next ask for input. E.g. if you ask for and only read one character, then any additional characters the user enter, will remain in the buffer. This may then taint any later gathering of input from the user. To prevent this, you call cin.flush() after reading what you want, thus making sure the buffer is empty for next time you ask for input.
?
2017-02-20 20:27:13 UTC
Cin Flush


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