The getline() function is used to read a line of data from text input and store it in the specified string until a newline character or end-of-file character is encountered. Its parameters include an istream object pointing to the input stream and a string object used to store the read data, and returns an istream reference pointing to the input stream object. If a row is read successfully, the status bit of the input stream object is goodbit, otherwise it is failbit.
Usage of getline() in C
getline() function is used to read a line of data and store it in the specified string. It reads data as text input until a newline character or end-of-file character is encountered. The syntax is as follows:
<code class="cpp">istream& getline(istream& str, string& strObj);</code>
Parameters
Return value
getline() function returns an istream reference pointing to the input stream object. The status bit of the istream object is goodbit if a line is successfully read, otherwise it is failbit.
Example
<code class="cpp">#include <iostream> #include <string> using namespace std; int main() { string myString; cout << "Enter a line of text: "; getline(cin, myString); cout << "The entered text is: " << myString << endl; return 0; }</code>
Execution process
Notes
The above is the detailed content of How to use getline in c++. For more information, please follow other related articles on the PHP Chinese website!