Home > Article > Backend Development > Usage of cin.getline in c++
The cin.getline() function is used to read a line of text from standard input into the specified string variable. Steps: Declare a character array to store text. Call cin.getline() on the cin object, passing the character array and the maximum number of characters. The terminating character '\0' is automatically added to the end of the text being read.
Usage of cin.getline() in C
Introduction to cin.getline()
cin.getline() is a function in C that reads a line of text from the standard input (cin) and stores it in the specified string variable. It is a member function and belongs to the istream class, so it needs to be called on the cin object.
Syntax
<code class="cpp">istream& cin.getline(char* str, int num);</code>
Parameters
Return value
cin.getline() returns a reference to an istream object that points to the input stream.
Usage
To read a line of text using cin.getline(), follow these steps:
Example
<code class="cpp">char name[50]; // 从用户读取姓名 cin.getline(name, 50); cout << "Hello, " << name << "!" << endl;</code>
Note
The above is the detailed content of Usage of cin.getline in c++. For more information, please follow other related articles on the PHP Chinese website!