Getline () function

Source: Internet
Author: User

This is in C + +

The prototype of Getline () is istream& getline (IStream &is, String &str, char delim);

Where IStream &is represents an input stream, for example cin;string&str means that the string read from the input stream is stored in the string (can be arbitrarily named, str or whatever); Char Delim indicates that the character is encountered to stop reading, If not set, the system defaults to the character ' \ n ', which is the carriage return newline character (encountered carriage return stop read in).

So what happens if Getline () is used as a while judgment statement? Let's analyze the while (Getline (cin,line)) statement Note that the default carriage return stops reading here, press CTRL + Z or type EOF enter to exit the loop. In this statement, first getline reads the characters from the standard input device and then returns to the input stream CIN, noting that it is CIN, so the true judgment object of the While judgment statement is CIN, which is to determine whether there is currently a valid input stream. In this case, I think as long as your computer is not poisoned not insane how can your input stream not be effective? So in this case no matter how you enter the loop, because your input stream is valid, cannot jump out of the loop. However, some students mistakenly think that while the judgment of the sentence is line (that is, line is empty), and then want to go through the direct carriage (that is, enter an empty line) out of the loop, but found how also can not jump out of the loop. This is because your return will only terminate the read-in operation of the Getline () function. The Getline () function terminates with a while () judgment (that is, if the input stream is valid, your input stream is of course valid and satisfies the condition), so the Getline () function is run

Getline () function

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.