Use of TWO cin.get()

Some IDEs close the window as soon as the program finishes execution, and some leave it open.To see the output, you must place some additional code at the end of the program:

cin.get(); // add this statement

But, is some rare cases, cin.get() is used two times consecutively; without it the output is not shown.

cin.get(); // add this statement
cin.get(); // and maybe this, too

Why are two cin.get() needed in some cases? :)
Using operator >>, leaves a newline char in the stream. The first .get() is meant to remove that.
Check these:
http://forums.devarticles.com/c-c-help-52/why-need-two-cin-get-27105.html
http://social.msdn.microsoft.com/Forums/ar/vclanguage/thread/d6ed9a83-dd65-4075-b521-2b148b7294c9

The problem is that cin.get(); reads one character. In the perfect case it's your "Enter press" at the end of the program. But if a character is still in the input buffer (not flushed) like '\n', cin.get () will read it, so you will need another cin.get() to read your "Enter press".

There is also a sticky thread on "Closing Down The Console" in the Beginner forum.
Topic archived. No new replies allowed.