C++ getline() Function (3 Syntax variation examples)

What is C++ getline() function?

The getline() function is:

  • Defined in the <string.h> header file
  • Is used to take user input in single as well as multi-lines.
  • A user can enter single or multiple words string
  • The characters are extracted from the input stream by getline() function. These characters are appended to the string object until the delimiter character is met. (Syntax section below will make it clearer)
  • The cin object can also be used to take user input (as shown in many examples of our C++ tutorials), however, it does not allow multiple lines or words – terminates where whitespace is met.

Syntax for using getline() function in C++

There are three variations in using the getline() function.

Syntax 1:

istream& getline( istream& is, string& str, char delim );

Syntax 2:

istream& getline( istream& is, string& str );

There:

  • is: specifies the input stream i.e. from where to read the input.
  • str: After reading from the stream, the input is stored in this string object.
  • delim: The getline() function stop reading further as this given delimiter is met in the input string.

Difference:

You may notice, the only difference between the above two syntaxes is the third parameter. The first syntax contains char delim as the third parameter.

So, you may specify a delimiter of your choice. For example, dot, comma, etc.

In the case of the second syntax, you may not specify the delimiter, so the default is used as the delimiter.

Getline() function with character array

There is another syntax for using the getline() function with a character array:

istream& getline(char* , int size);

This is explained with an example in the last section of this tutorial.

An example that uses getline() function (two params)

#include <iostream>

#include <string>

using namespace std;




int main() {

  string str_name;

  cout << "What is your full name?" << endl;

  getline(cin, str_name);

  cout << "Hello and Welcome, " << str_name << "!" << endl;

}

Sample output:

CPP-getline

In the above program:

  • A string variable is declared
  • In the getline() function, we used two parameters.
  • cin is for console input
  • The entered name is displayed (try multiple words)

What if simply used cin rather getline() example

In the program below, we simply used input stream cin and displayed the value of a variable. Enter your full name and you can see the difference:

#include <iostream>

#include<string.h>

using namespace std;

int main()

{

//Creating variable

string str_name;

std::cout << "What is your full name? " << std::endl;




//Taking user input

cin>>str_name;




//Display variable value

cout << "Hello and Welcome, " << str_name << "!" << endl;

return 0;

}

Sample output:

CPP-cin

In the sample output, you can see I entered the name “Mike Millan”. However, the program only displayed the first name i.e. Mike. This is because; C++ compiler terminates it as it finds a whitespace in the string.

This is where getline() function plays its role.

An example of using three parameters in getline() function

As mentioned earlier, you may specify a delimiter of your choice in the third parameter of the getline() function.

In this example, I used the dot (full-stop) a delimiter character in the getline(). See the code and a sample output below:

#include <iostream>

#include <string>

using namespace std;




int main() {

  string str_fruits;

  cout << "Enter your favourite fruits?" << endl;




  //getline function with dot delimiter

  getline(cin, str_fruits,'.');

  cout << "Hello and Welcome, " << str_fruits << "!" << endl;

}

Sample output:

CPP-cin-dilimeter

in the output, you can see as we entered three fruit names as input. As I entered a dot after banana, the string terminated and it only displayed two fruit names.

Using the comma as a delimiter example

Similarly, you may use other characters as delimiters in the getline() function. For example, a comma, semicolon, a , b, c, space,  etc.

The following example uses a comma as a delimiter character:

#include <iostream>

#include <string>

using namespace std;




int main() {

  string str_fruits;

  cout << "Enter your favourite fruits?" << endl;




  //getline function with dot delimiter

  getline(cin, str_fruits,',');

  cout << "My fruits are:, " << str_fruits << "!" << endl;

}

Sample output:

CPP-getline-comma

An example of using getline() with character array

As mentioned in the section above, there is another syntax for using the getline() function with a character array. i.e.:

istream& getline(char* , int size);

The following example shows a C++ program using it:

#include <iostream>

#include<string.h>

using namespace std;

int main()

{

//Declaring country array

char countries[50];




cout<< "Country Names: ";




//Specifying array in getline()

cin.getline(countries, 50);




//Display entered country names

cout << "\nThe country names are :"<<countries << std::endl;

return 0;

}

CPP-getline-array

In the program:

  • We declared a character array to be used in the getline() function
  • In the getline() implementation (cin.getline(countries, 50);), the array is specified along with the size in integer.
  • This size parameter acts as the delimiter whereas entered value cannot exceed this limit.
  • If input increases the limit, the additional characters are terminated.

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!