Skip to main content
Engineering LibreTexts

11.2: C++ Input- getline()

  • Page ID
    29100
  • getline (string) in C++

    The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. If no delimiter is specified, the default is the newline character at the end of the line. The input value does NOT contain the delimiter. While doing so the previously stored value in the string object str will be replaced by the input string if any.

    The getline() function can be represented in two ways:

    1. Syntax:
      // inputStream is like cin
      string someString;
      char delimiter;
      
      getline(inputStream, someString, delimiter);

      Parameters:

      • inputStream: It is an object of istream class and tells the function about the stream from where to read the input from.
      • someString: It is a string object, the input is stored in this object after being read from the stream.
      • delimiter: It is the delimitation character which tells the function to stop reading further input after reaching this character.

      Return Value: This function returns the same input stream as is which is accepted as parameter.

    2. Syntax:
      getline (inputStream, someString);

      The second declaration is almost the same as that of the first one. The only difference is, the latter have an delimitation character which is by default new line(\n)character.

      Parameters:

      • inputStream: It is an object of istream class and tells the function about the stream from where to read the input from.
      • someString: It is a string object, the input is stored in this object after being read from the stream.

      Return Value: This function returns the same input stream as is which is accepted as parameter.

    Here is an example of using getlin() function. 

    // C++ program to demonstrate getline() function 
    
    #include <iostream> 
    #include <string> 
    using namespace std; 
    
    int main() 
    { 
        string str; 
    
        cout << "Please enter your name: \n"; 
        getline(cin, str); 
        cout << "Hello, " << str << " welcome to CSP 31A!" << endl; 
    
        return 0; 
    } 
    

    In this code, we simply perform a getline()  using cin as out input stream and a variable named str to hold the input obtained by getline(), then we pass this variable to the cout routing.

    Input:

    Pat Mac
    

    Output:

    Hello, Pat Mac welcome to CSP 31A!

     Example 2: We can use getline() function to split a sentence on the basis of a character. Let’s look at an example to understand how it can be done.

    // C++ program to understand the use of getline() function 
    #include <bits/stdc++.h> 
    using namespace std; 
    
    int main() 
    { 
        string myString, tempString; 
    
        // Read some input from the keyboard
        getline(cin, myString); 
    
        // This turns our string into a stream
        stringstream myStream(myString); 
        
        // Using the newly created stream we read if up to the first space character
        // The next read starts at the character 
        while (getline(myStream, tempString, ' ')) 
        { 
            cout << tempString << ' '; 
        } 
        cout << endl; 
        return 0; 
    } 
    

    We read from the keyboard and put those characters into the string myString. Then we turn that string into a stream using the stringstream() function. Lastly we use a while loop to take our input, and read up until the first space character - since that is the delimiter we specify in the getline() function. that word in placed in tempString. We output that string. Then we go back to the top of the loop and grab the next set of characters, until the next space, assigned that to tempString and output that string. we continue doing this until we reach the end of the input in myStream. When we reach the end of the input the condition becomes false and we stop the while loop.

    Notice when we output each string we had to add the space, when we use a delimiter in the getline() function, that character gets thrown away.

    Pat Mac - welcome to this glorious place
    
    if we don't include the space at the end of the cout statement the output looks like
    
    PatMac-welcometothisgloriousplace
    

     Adapted from:
    "getline (string) in C++" by Harsh Agarwal and Faisal Al Mamun, Geeks for Geeks is licensed under CC BY-SA 4.0