Going through a text to help cement my knowledge of C++ but found code that wasn't explained and looks kinda odd to me.

#include <iostream>
#include <string>
int main()
{
	using namespace std;

	char ch;
	int count = 0;

	cin.get(ch);
	while(ch != '\n')
	{   
		cout << ch;
		++count;
		cin.get(ch);
	}
	
	cout << "Done Typing!\n You typed " << count << " words\n";

	return 0;
}

pretty basic, but I have a question about cin. Does it only freeze the console until enter is pressed if no input stream is detected? When the program starts up it let's me type whatever I wish till I press enter, but even though the same function is called during the while loop it just iterates over buffered input characters until '\n' is reached.

Just odd behavior I hadn't seen from cin and would like some input on exactly why this is, if possible.

Recommended Answers

All 6 Replies

It was designed that way -- cin is a carry-over from C's getchar() and related functions, which behave the same way. There are no standard functions in either C or C++ that return to the program immediately when a key is pressed. If you want that behavior then you have to resort to non-standard functions such as those found in conio.h for MS-Windows/MS-DOS. *nix would use curses, and an MS-Windows port is available called pdcurses. On MS-Windows you could also use win32 api console functions.

So what i'm assuming is correct? It checks the input stream for characters and if it can't find one it freezes the console to allow input till enter is pressed?

I just read the code and didn't think it was going to work based on how I typically saw cin. whatever freeze the console so I was surprised. Just looking for a good functional knowledge of how it works, let's me use it more creatively/efficiently later down the road.

Yes, you are correct about that behavior. There is really a little more to it, but that explanation will do for now.

cin is a buffer, it needs to flush, just like cout. There is no guarantee that cin will flush every time you press a key but it is guaranteed that it will flush when you hit enter (just like cout is guaranteed to flush when you write std::endl or "\n"). That's why it will freeze until you press enter, but it could also unfreeze earlier when you have typed something but not hit enter yet (this rarely occurs but it can, like if you type a few letters, then minimize the terminal window and maximize again, this could trigger cin to flush the few letters you had typed before and unfreeze execution). For instant key-stroke detection you need OS-specific stuff or a GUI platform of some kind. Cin is meant for command-line instructions: basically, "type a command and press <enter>".

BTW cout is independent from cin so waiting for a char or string input from cin will not freeze cout, but it will freeze execution of the calling thread. In the mean-time, another thread can output anything to cout and it will never show up in cin because although they share a terminal they are separate buffers. So the console is not freezed, your program is, as long as you have only a single thread.

>>cin is a buffer, it needs to flush, just like cout
No it is not a buffer -- it is a c++ class. And cin can not be flushed. flush() only works on output streams, never input streams.

yeah.. cin is a class, sure, that receives the character streaming from a buffer. You're right, you can't flush it, because only the writer of a buffer can flush it (computer science 101), that was my point. Only the user can trigger flushing of the input buffer so there is no way for you in the program to try and flush it, you can only wait for the user to do an action that the operating system (or terminal / console application) will deem worthy of a flush, like pressing <enter>.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.