Discussion on the problems related to while (CIN>>STR) and Ctrl z in C + +

Source: Internet
Author: User
Tags readable

For the while (CIN>>STR) and Ctrl z issues, the following explanation is available online:

--------------------------------------------------------------------------------------------------------------- ---------------

The input (CIN) buffer is a row buffer. When you enter a string of characters from the keyboard and press ENTER, the characters inode are first sent to the input buffer for storage. Each time you press ENTER, CIN
The input buffers are detected for readable data.

CIN also checks to see if the keyboard has a CTRL + Z or CTRL+D key pressed as the end-of-flow flag, which is checked in two ways: block and non-blocking.

The blocking check method refers to whether the previous Ctrl + Z key Press is checked only after the return press is pressed, and the non-blocking style refers to the way in which the response is pressed immediately after ctrl+d. If a character has been entered from the keyboard before pressing Ctrl+d, the CTRL+D function is equivalent to a carriage return, which means that the characters are sent to the input buffer for reading, and ctrl+d no longer functions as a stream terminator. If there is no keyboard input before pressing Ctrl+d, then CTRL+D is the signal for the end of the stream.
The blocking style has one feature: it is possible to detect if there is a CTRL + Z press before the carriage return. Another feature is that if there is readable data in the input buffer, CTRL + Z is not detected (because there is data to be read and cannot be considered at the end of the stream). It is also important to know that CTRL + Z produces not a normal ASCII value, that is, it produces not a character, so it will not be stored in the input buffer as other characters entered from the keyboard.

Windows system generally uses the blocking check Ctrl + Z, Unix/linux system generally adopts non-blocking inspection ctrl+d.

--------------------------------------------------------------------------------------------------------------- --------------------

The above means that in the window to press CTRL z + Ente to end the loop, and on the UNIX system just press CTRL D

At the same time, summarize some experimental results on this machine. Windows 7 + vs2010

Approximate procedure:

String str;

while (CIN >> str)

cout<<str<<endl;

Summary of the experimental results:

When you enter ASD (CTRL + Z) (enter), you continue to require keyboard input and then enter FF (enter), and the result output is ASD→FF

When input ASD (ctrl + Z) sad (enter), the same request input, and then enter FF (enter), the result output is still Asd→ff,ctrl Z behind the sad is ignored

So, there are some rules to summarize.

(1) When the readable data appears before pressing CTLR Z, CTRL z+enter becomes the ' → ' character, while the data behind the CTRL z in the peer is ignored.

And because Ctrl z+enter becomes the ' → ' character, so Cin>>str's read is not over, continue to request input and press ENTER to announce the end of a read.

(2) If you need to end the loop, its method must be the line input is the CTRL Z+enter,ctrl Z can not have any characters, even cin>>str not read the space is not.

Finally, the problem has not been tested in UNIX systems.

Discussion on the problems related to while (CIN>>STR) and Ctrl z in C + +

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.