How to exit while (cin & gt; ch)

Source: Internet
Author: User

Method: Press enter, Ctrl + z on the new line, and press Enter. It is invalid if Ctrl + z is entered and then press Enter.
Cause:
The input buffer is a row buffer. After entering a string of characters on the keyboard and pressing enter, these characters are first sent to the input buffer for storage. Every time you press the Enter key, cin. get () checks whether readable data exists in the input buffer. Cin. get () also checks whether there are Ctrl + Z or Ctrl + D keys on the keyboard as the stream stop sign. There are two ways to check the result: blocking and non-blocking.

Blocking Check means that the Ctrl + Z combination key is used only after the Enter key is pressed, the non-blocking mode refers to the method of responding immediately after pressing Ctrl + D. If you have entered characters from the keyboard before pressing Ctrl + D, Ctrl + D serves as a carriage return, that is, sending these characters to the input buffer for reading, in this case, Ctrl + D no longer serves as the stream Terminator. If you do not have any keyboard input before pressing Ctrl + D, Ctrl + D indicates the end of the stream.

In Windows, blocking-type Ctrl + Z and non-blocking-type Ctrl + D are usually used in Unix/Linux systems. The main user is in Windows, so the blocked Ctrl + Z is used to mark the end of the stream.

This blocking method has one feature: it is possible to detect whether Ctrl + Z is pressed before pressing the carriage return. 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 at the end of the stream ). Another thing we need to know: Ctrl + Z does not produce a common ASCII code value, that is, it does not produce a character, therefore, it is not stored in the input buffer as other characters entered from the keyboard. After understanding these points, you can explain the questions raised by the landlord.

After entering abcd ^ z on the keyboard and pressing enter, it will be handled in the Windows system as follows: Due to the press ENTER function, the previous abcd and other characters will be sent to the input buffer (Note: As mentioned above, ^ z does not generate characters, so it is not stored in the input buffer, and there is no ^ z in the buffer ). In this case, cin. get () detects that data already exists in the input buffer (so it no longer checks whether ^ z input exists), and reads the corresponding data from the buffer. If all the data is read, the input buffer is empty again, and cin. get () waits for the new input. It can be seen that even if ^ z is pressed, the stream will not end because there are other input characters (abcd) before this.

Therefore, the input stream end condition is: ^ z cannot have any character input (except press Enter), otherwise ^ z cannot end the stream.


From deserve your kick

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.