Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does cin work?

Tags:

c++

iostream

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

like image 735
Pavel Avatar asked Apr 28 '16 12:04

Pavel


People also ask

How does Cin get work?

get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found.

What is the work of CIN in C++?

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file.

How does cin and cout work in C++?

cin is an object of the input stream and is used to take input from input streams like files, console, etc. cout is an object of the output stream that is used to show output. Basically, cin is an input statement while cout is an output statement. They also use different operators.

What does the cin statement do?

The cin object in C++ is an object of class iostream. It is used to accept the input from the standard input device i.e. keyboard. It is associated with the standard C input stream stdin.


Video Answer


2 Answers

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.

like image 71
Angew is no longer proud of SO Avatar answered Nov 15 '22 18:11

Angew is no longer proud of SO


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.

like image 40
GMichael Avatar answered Nov 15 '22 17:11

GMichael