Pavel
Pavel

Reputation: 5876

How does cin work?

I ran this code

char c;
cin >> c;
cout << c;
cin >> c;
cout << c;

and wrote to the console ab, the pressed enter. So I got ab at the next line. But I can't understand how it works. Before pressing enter the program doesn't read anything, right? After pressing, it reads a, save it to char c, then reads char c, writes a to the console. It's OK. But how can it read b being at the second line? It isn't b at the second line

Upvotes: 9

Views: 19831

Answers (4)

cin is the standard input stream. The streaming nature is vital in-depth for understanding C++ I/O.

By default, doing cin >> x means:

From the point currently in the stream, skip any whitespace which might be there and then keep reading as long as possible & necessary to get a valid representation of x.

Let us disregard for a moment the fact that input comes from the keyboard. The content of the stream at start is a b LINEFEED. You execute cin >> c, which will read the first character, a, from input. That's enough to fill in c, so reading stops. The cin stream now contains b LINEFEED. The variable c is then written to standard output.

Another cin >> c comes next, so one more character is read (this time b). Again, one character is enough, so reading ends and the stream contents is just LINEFEED. The b is then sent to the standard output stream.

The fact that the standard input and standard output streams are normally tied to the console does not affect their internal working in any way. cin doesn't "forget" what was in it just because some output appeared on the screen in the meantime. In particular, cin reads the keyboard, not "characters on the console." It just so happens that pressing keys both echoes them on the console and feeds them to cin.

So the fact that your program has output the character a in the meantime has no effect on the contents of the cin stream.

Upvotes: 21

Sergei
Sergei

Reputation: 550

cin is a blocked input. Whatever comes from the keyboard is stored in a buffer. When you press enter the system passes the buffer to the application code (std::cin code). Operator >> will decide how much to read from that buffer - one char, string, int, float etc. Depends on the type of the operand.

Upvotes: 2

GMichael
GMichael

Reputation: 2776

cin and cout are buffered streams. Both 'a' and 'b' goes into the input buffer when you press enter. The '>>' operator reads from that buffer (one char at a time in your case). The '<<' writes to the output buffer. The only thing that should surprise you is that you see "ab" on output without printing "\n" (the latter symbol should flush the contents of the output buffer to the terminal).

In short, both cin and cout are buffers. Input and output operators work with those buffers. Newline symbol initiates the data transfer from real input to input buffer and from output buffer to the real output.

There more thing about I/O you can learn.

Upvotes: 2

Hatted Rooster
Hatted Rooster

Reputation: 36463

Basically, cin has an overload for data type char to only grab 1 character from the input stream, so your program basically goes like this :

char c;
cin >> c; // reads 'a' from the input stream (input stream contains 'ab\n')
cout << c; // prints 'a'
cin >> c; // changes c to 'b' (reads 'b')
cout << c; // prints 'b'

Upvotes: 0

Related Questions