On the while of cin problem in c++

  • 2020-05-19 05:15:48
  • OfStack

The xp system USES dev-cpp for programming, the statement while(cin) > > str), str is a type of string. Enter several string in line 1 and add ctrl+z at the end. The input does not end until you enter ctrl+z. I don't understand anything, please see the explanation.

The input buffer is the row buffer. When you enter a string of characters from the keyboard and press enter, these characters are first sent to the input buffer for storage. Whenever the enter key is pressed, cin.get () detects whether there is readable data in the input buffer. cin.get () also checks whether there is an Ctrl+Z or an Ctrl+D key on the keyboard as a sign-off for the end of the stream. There are two ways to check this: blocking and non-blocking.

The blocking check method refers to the way in which the previous Ctrl+Z combination is checked only after the enter key is pressed, and the non-blocking style refers to the way in which the Ctrl+D combination is pressed and responded immediately. If characters are entered from the keyboard before Ctrl+D is pressed, Ctrl+D ACTS as a carriage return, sending the characters to the input buffer for reading, at which point Ctrl+D no longer ACTS as a stream terminator. If there is no keyboard input before pressing Ctrl+D, Ctrl+D is the end of the stream signal.

In the Windows system, blocking check is generally adopted in 1, and non-blocking check is generally adopted in Ctrl+D in the Unix/Linux system. The landlord is on the Windows system, so the blocking Ctrl+Z is used to mark the end of the flow.

This blocking method has one feature: it is only possible to detect whether Ctrl+Z has been pressed before pressing enter. Another feature is that if there is readable data in the input buffer, Ctrl+Z will not be detected (because there is data to read, it cannot be considered to be at the end of the stream). There is one more thing to know: Ctrl+Z does not produce a normal ASCII code value, that is, it does not produce a single character, so it will not be stored in the input buffer like other characters entered from the keyboard. Understand these points after you can come to explain the problem raised by the building.

Enter abcd^z from the keyboard and press return. On the Windows system, it is handled like this: due to the effect of press return, characters such as abcd before are sent to the input buffer (note: as mentioned above, ^z does not generate characters, so it is not stored in the input buffer, there is no ^z in the buffer). At this point, cin.get () detects that data already exists in the input buffer (so it no longer checks for ^z input) and reads the corresponding data from the buffer. If all are read, the input buffer becomes empty again, cin.get () waiting for new input. As you can see, despite the ^z press, the stream does not end because there are other input characters (abcd) before it does.

Thus, the end of the input stream is conditional on ^z not having any character input before it (except carriage return), otherwise ^z does not function as the end of the stream.


Related articles: