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 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 running, 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, such as CIN; string & STR indicates that the string read from the input stream is stored in this string (you can name it by yourself, STR can be used); char delim indicates that this character is used to stop reading, if this character is not set, the system defaults to '\ n', that is, the carriage return line break (when you press enter to stop 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 ). Then the program runs as follows: Please CIN a line: You are the # best! The line you give is: you are the and here set the terminator to #. It doesn't matter if you enter several carriage returns during input, and the input stream will still be read, for example: please CIN a line: you are the best! // Enter a carriage return to wrap thank you! # // Stop reading the line you give is: you are the best! // The line feed is still read and the output is thank 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, you cannot jump out of the cycle regardless of your input, because your input stream is valid and cannot skip the cycle. 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...
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.