How does while (STD: CIN> Val) end?

Source: Internet
Author: User

Reference: 62053816/

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

CIN is the standard input stream object in the C ++ programming language, that is, the object of the istream class. CIN is mainly used to read data from standard input. The standard input here refers to the terminal keyboard. In addition, cout is the object of the stream, that is, the object of the ostream class. cerr is the object of the standard error output stream and also the object of the ostream class. The standard output here refers to the terminal keyboard, and the standard error output refers to the terminal screen.

When understanding the CIN function, you have to raise the standard input buffer. When you enter a string from the keyboard, You need to press the Enter key to send the string to the buffer zone. Then, press the Enter key (\ r) it will be converted into a linefeed \ n. This linefeed \ n will also be stored in the CIN buffer and calculated as a character! For example, if we press the 123456 character string on the keyboard and press the Enter key (\ r) to send the character string to the buffer zone, the number of bytes in the buffer zone is 7 instead of 6.

When the buffer zone is empty, the member function of CIN blocks and waits for data to arrive. Once there is data in the buffer zone, this triggers the member function of CIN to read data. When CIN> reads data from the buffer, if the first character in the buffer is a space, tab, or line feed delimiter, CIN> ignores and clears the delimiter, continue to read the next character. If the buffer is empty, continue to wait. However, if the read is successful, the delimiters behind the characters are left in the buffer zone, and CIN> is not processed.

When an istream object is used as a condition, the result is to check the stream status. If the stream is valid, the detection is successful and true is returned. WhenFile terminator or invalid input(For example, when a character is used as the input of an integer number), the object status of istream changes to invalid.

All in all:

(1) When entering data, You need to press the Enter key to send the data to the cache, so that CIN can read the data in the cache.

(2) When CIN encounters a file terminator (CTRL + Z in Windows,For Unix: Ctrl + d), Or invalid input to make the CIN status invalid.

The test code below will explain the above issues.

Code purpose: count the number of consecutive occurrences of data in the input value.
---------------------
Rabbit
Source: csdn
Original: 62053816
Copyright Disclaimer: This article is an original article by the blogger. For more information, see the blog post link!

How does while (STD: CIN> Val) end?

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.