SoFunction
Updated on 2025-03-10

A brief discussion on the while(cin) issue in c++

In the xp system, dev-cpp is used for programming. The statement while(cin>>str), str is a string type. Enter several strings in a line, add ctrl+z at the end, and the input does not end. Unless you enter and exit the line, enter ctrl+z before you can jump out of the input. I haven't understood anything, please read the explanation.

The input buffer is a line buffer. When a string of characters is entered from the keyboard and press Enter, these characters are first sent to the input buffer to store. Whenever the Enter key is pressed, ()   will detect whether there is readable data in the input buffer. ()   It will also check whether there is a Ctrl+Z or Ctrl+D key pressed on the keyboard, and there are two ways to check: blocking and non-blocking.

The blocking check method refers to whether there is any previous one only after the Enter key is pressed.Ctrl+Z The key combination is pressed to check. The non-blocking style refers to pressing.Ctrl+DWay to respond immediately afterwards. If pressingCtrl+D   Characters have been entered from the keyboard before, then Ctrl+DThe function of this is equivalent to carriage return, that is, send these characters to the input buffer for reading and use.Ctrl+DNo longer function as a stream ending symbol. If pressCtrl+DIf there was no keyboard input before, thenCtrl+D  It is the signal that the stream ends.

Blocking checks are generally used in Windows systems. Ctrl+Z and Unix/Linux systems are generally used in non-blocking checks are generally used in Ctrl+D. The poster is on Windows system, so he uses blocking Ctrl+Z to identify the end of the stream.

This blocking method has a feature: it is possible to detect whether Ctrl+Z is pressed before this. Another feature is that if there is readable data in the input buffer, Ctrl+Z will not be detected (because there is data to be read, it cannot be considered to be at the end of the stream). Another thing to know: Ctrl+Z does not produce an ordinary ASCII code value, which means it does not produce a character, so it will not be stored in the input buffer like other characters entered from the keyboard. After understanding these points, you can explain the questions raised by the poster.

Enter abcd^z from the keyboard and add    After entering the carriage, it is handled like this on the Windows system: Due to the carriage return, the previous characters such as abcd are sent to the input buffer due to the carriage return.(Note: As mentioned above, ^z will not generate characters, so it will not be stored in the input buffer, and there is no ^z in the buffer).At this time, ()   detected that there is already data in the input buffer (so no longer checks whether there is input from ^z  ), so the corresponding data is read from the buffer. If all reads, the input buffer becomes empty again, ()   Wait for new input. It can be seen that despite pressing ^z, the stream will not end because there are other input characters (abcd) before this.

Therefore, the condition for the input stream end is: ^z   There cannot be any character input before (except for carriage return), otherwise ^z   will not function as the stream end.

The above brief discussion on while (cin) issues in c++ is all the content I share with you. I hope you can give you a reference and I hope you can support me more.