Skip to main content
Engineering LibreTexts

11.2: C++ Input- getline()

  • Page ID
    29100
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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


    This page titled 11.2: C++ Input- getline() is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?