getline (string) in C++


It is used to extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character). The declaration is like:

basic_istream& getline (char_type* s, streamsize n );
basic_istream& getline (char_type* s, streamsize n, char_type delim);

The parameters are ‘s’ pointer to an array of characters, where the extracted characters are stored as a c_string. Next parameter is ‘n’ this is the maximum number of characters to write (including the terminating character). The third parameter is ‘delim’ Explicit delimiting character. The operation of extracting successive characters stops as soon as the next character to exact compares equal to this (using traits_type::eq)

This function returns the basic_istream object (*this).

Example

 Live Demo

#include <iostream>
using namespace std;
int main () {
   char name[256], title[256];
   cout << "Please, enter your name: ";
   cin.getline (name,256);
   cout << "Please, enter your favourite movie: ";
   cin.getline (title,256);
   cout << name << "'s favourite movie is " << title;
}

Output

Please, enter your name: Jack
Please, enter your favourite movie: The Boss Baby
Jack's favourite movie is The Boss Baby

Updated on: 30-Dec-2019

638 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements