C++ Getline

In C++, the getline is a built-in function defined in the <string> header file that allows reading a full line of text from an input stream. The cin is an object which is used to take input from the user but does not allow it to take the input in multiple lines.

We use the getline function to accept the multiple lines. It enables reading a complete line, including whitespace characters, which makes it a powerful tool for handling more complex input scenarios in C++ .

Syntax of getline function:

1. The first way of declaring is to pass three parameters.

Example

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

The above syntax contains three parameters, i.e., is, str , and delim .

Where,

is: It is an object of the istream class that defines from where to read the input stream.

str: It is a string object in which string is stored.

delim: It is the delimiting character.

Return value

This function returns the input stream object, which is passed as a parameter to the function.

2. The second way of declaring is to pass two parameters.

Example

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

The above syntax contains two parameters, i.e., is and str . This syntax is almost similar to the above syntax; the only difference is that it does not have any delimiting character.

Where,

is: It is an object of the istream class that defines from where to read the input stream.

str: It is a string object in which string is stored.

Return value

This function also returns the input stream, which is passed as a parameter to the function.

C++ getline Function Example:

Let's take a simple example where we take the user input without using the getline function.

Example

Example

#include <iostream>  

#include<string.h>  

using namespace std;  //using standard namespace

int main()  //main function

{  

string name; // variable declaration  

std::cout << "Enter your name :" << std::endl;  

cin>>name;  

cout<<"\nHello "<<name;  

return 0;  

}

Output:

Output

Enter your name :

John Miller

Hello John

Explanation:

In this example, we take the user input by using the statement cin>>name, i.e., we have not used the getline function. After that, we gave the name 'John Miller' as user input, but only 'John' was displayed. Therefore, we conclude that cin does not consider the character when the space character is encountered.

Let's resolve the above problem by using the getline function.

Example

Example

#include <iostream>  

#include<string.h>  

using namespace std;  //using standard namespace

int main()  //main function

{  

string name; // variable declaration.  

std::cout << "Enter your name :" << std::endl;  

getline(cin,name); // implementing a getline() function  

cout<<"\nHello "<<name;  

return 0;

}

Output:

Output

Enter your name :

John Miller

Hello John Miller

Explanation:

In the above code, we have used the getline function to accept the character even when the space character is encountered. After that, we can observe that both the words, i.e., John and Miller, are displayed, which means that the getline function considers the character after the space character also.

Using the getline with a Delimiter to Stop Input at a Space in C++

Example

Example

#include <iostream>  

#include<string.h>  

using namespace std;  

int main()  

{  

string profile; // variable declaration  

std::cout << "Enter your profile :" << std::endl;  

getline(cin,profile,' '); // implementing getline() function with a delimiting character.  

cout<<"\nProfile is :"<<profile;  

}

Output:

Output

Enter your profile :

Software Developer

The profile is: Software

Explanation:

In the above code, we take the user input by using the getline function, but this time, we also add the delimiting character('') in a third parameter. Here, the delimiting character is a space character, which means the character that appears after space will not be considered.

Using C++ getline with Two Parameters:

In C++, the getline function reads until it faces a new line (\n) by default. It stores the input in the given string. It is the most commonly used form of getline when we only want to read the entire rows of text.

Syntax:

It has the following syntax:

Example

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

C++ Getline function Example using two parameters:

Example

Example

#include <iostream>

#include <string>

using namespace std;

int main() {

    string fullName;

    cout << "Enter your full name: ";

    getline(cin, fullName);

    cout << "Hello, " << fullName << "!" << endl;

    return 0;

}

Output:

Output

Enter your full name: John Smith

Hello, John Smith!

Explanation:

In this example, it asks the user to enter their full name. After that, the getline (cin, full name) function reads full input, including spaces (e.g., "Alice Johnson"). Finally, it greets the user using his full name.

Using C++ getline with Three Parameters:

The three-parameter version of Getline allows us to specify a custom delimitator in addition to the default Newline (\n). It is useful when the data field is distinguished by special characters such as a comma, semiconduram or pipe.

Syntax:

It has the following syntax:

Example

getline(input_stream, destination_string, delimiter_char);

C++ Getline function Example using three parameters:

Example

Example

#include <iostream>

#include <string>

using namespace std;

int main() {

    string data;

    cout << "Enter some text ending with a '#': ";

    getline(cin, data, '#');  // Using getline with delimiter '#'

    cout << "You entered: " << data << endl;

    return 0;

}

Output:

Output

Enter some text ending with a '#': Hello World, this is C++ Getline #

You entered: Hello World, this is C++ Getline

Explanation:

This C++ program reads the input until the # character is faced. # is not included in the stored string.

Getline Character Array:

We can also define the getline function for the character array, but its syntax is different from the previous one.

Syntax:

It has the following syntax:

Example

istream& getline(char* , int size);

In the above syntax, there are two parameters: one is char*, and the other is size.

Where,

  • char*: It is a character pointer thacpp tutorials to the array.
  • Size: It acts as a delimiter that defines the size of the array means input cannot cross this size.
  • C++ Getline Character Array Example:

    Example

    Example
    
    #include <iostream>  
    
    #include<string.h>  
    
    using namespace std;  
    
    int main()  
    
    {  
    
    char fruits[50]; // array declaration  
    
    cout<< "Enter your favourite fruit: ";  
    
    cin.getline(fruits, 50); // implementing the getline() function  
    
    std::cout << "\nYour favorite fruit is :"<<fruits << std::endl;  
    
    return 0;  
    
    }
    

Output:

Output

Enter your favourite fruit: Watermelon

Your favorite fruit is Watermelon

C++ Getline for Character Array and Exceeding Char Limit:

Example

Example

#include <iostream>

using namespace std;

int main() {

    char str[10];

    cout << "Enter a string: ";

    cin.getline(str, 10);

    cout << "You entered: " << str << endl;

    return 0;

}

Output:

Output

Enter a string: HelloWorldAgain

You entered: HelloWorld

Explanation:

The cin. getline(str, 10) function reads up to 9 characters and automatically appends a null character (\0) at the end. If input exceeds the limit, the extra characters are left in the input buffer, and only the allowed characters are stored.

Conclusion

In conclusion, the getline function in C ++ is an important tool when you need to read the entire lines of the input, including the spaces, either in a std:: string or in a character array. It first stops on whitespaces and eliminates the boundaries of the cin.

C++ Getline function Multiple Choice Questions

  1. What does the getline function mainly do in C ++?
  • Reads a single word
  • reads a single character
  • reads a full line until a newline character
  • ignores spaces and reads till the end
  1. Which major limitation of cin does getline solve?
  • Reading numerical value
  • reading input with whitespaces
  • reading character arrays
  • Except unwanted inputs
  1. When using getline, what happens if the input stream already contains a newline character before calling it?
  • It generally reads new input
  • It leaves Newline and waits for new input
  • It immediately reads an empty string
  • This causes a compilation-time error
  1. What will happen if we specify a delimiter in the gateline ?
  • Getline will ignore it and stop at Newline
  • Getline will stop reading when it encounters that delimiter
  • This will cause a segmentation fault
  • It will continue to read until the buffer is complete
  1. Which of the following describes the best getline when it works with character arrays?
  • It reads the size of the array
  • It automatically gives array resize.
  • It reads up to a specified size or until a delimiter
  • It always leaves the first character

Input Required

This code uses input(). Please provide values below: