C ++ receives strings from the keyboard, which is simple.

Source: Internet
Author: User

C ++ receives strings from the keyboard, which is simple.
C ++ learners may encounter a getline () function. For example, in C ++ premer, the second section of the standard string type is "reading the entire line of text with getline ". The program given in the book is as follows: int main (){String line:While (getline (cin, line ))Cout <line <endl;Return 0;} You will find that there is no way to skip the loop during the runtime, or even a variety of inexplicable errors. Why? Here I will give you a detailed explanation. First, let us introduce the getline () function (I personally think that Baidu Baike has not provided enough details). Baidu will find that the prototype of getline () is istream & getline (istream & is, string & str, char delim); istream & is indicates an input stream, for example, cin; string & str indicates storing the string read from the input stream in this string (you can name it by yourself, str); char delim indicates that reading of this character is stopped. If this character is not set, the system defaults to '\ n ', that is, the carriage return line break (when the carriage return stops reading ). For example, string line; cout <"please cin a line:" getline (cin, line ,'#'); cout <endl <"The line you give is:" line; then when I enter "You are the # best! ", The input stream is actually read-only into" You are the ", # the latter is not stored in line (it should be in the buffer zone ). The program running result is as follows:Please cin a line: You are the # best!The line you give is: You areThe Terminator is set to #. It does not matter if you enter several Carriage Return Characters During input. The input stream will still be read, for example:Please cin a line: You are the best! // Enter a line feedThank you! #// Stop reading The line you give is: You are the best! // The line feed is still read and OutputThank you! The above is a small instance of the getline () function. So what if getline () is used as the while judgment statement? Let's take a look at the while (getline (cin, line) statement. Note that the carriage return stops reading. Press Ctrl + Z or press enter to exit the loop. In this statement, getline first reads characters from the standard input device, and then returns the result to the input stream cin. Note that the value is cin. Therefore, the actual judgment object of the while statement is cin, that is, to determine whether a valid input stream exists. In this case, I think your input stream is not valid as long as your computer is not poisoned or nervous? In this case, no matter how you input the data, you cannot skip the loop because your input stream is valid. However, some students mistakenly think that the while statement's judgment object is line (that is, whether line is empty), and then they want to jump out of the loop by directly pressing the carriage return (that is, entering an empty line, however, we can't skip the loop. This is because your carriage return only terminates the reading operation of the getline () function. After the getline () function is terminated, while () is used to determine whether the input stream is valid. Of course, your input stream is valid and meets the conditions). Therefore, the getline () function is run. Therefore, you understand...

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.