Skip to main content
Engineering LibreTexts

11.2.1: C++ Input- More getline()

  • Page ID
  • \( \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}}\)

    getline() function and character array

    In C++, stream classes support line-oriented functions, getline() and write() to perform input and output functions respectively. getline() function reads whole line of text that ends with new line or until the maximum limit is reached. getline() is the member function of istream class and has the syntax:

    The function does the following operations:
    1. Extracts character up to the delimiter.
    2. Stores the characters in the buffer.
    3. Maximum number of characters extracted is size – 1.
    Note that the terminator(or delimiter) character can be any character (like ‘ ‘, ‘, ‘ or any special character, etc.). The terminator character is read but not saved into a buffer, instead it is replaced by the null character.

    // C++ program to show the getline() with 
    // character array 
    #include <iostream> 
    using namespace std; 
    int main() 
        char str[20]; 
        cout << "Enter Your Name::"; 
        // see the use of getline() with array str
        // will read until '\n' or 20 characters
        cin.getline(str, 20); 
        cout << "\nYour Name is:: " << str; 
        return 0; 

    Notice that the getline() function looks almost like a method of cin. This example does NOT use a is proper to use a delimiter if you desire.

    Input :

    Pat Mac 

    Output :

     Your Name is:: Pat Mac

    This code exhibits the write/put with cout. The cout.write will write out at most 9 characters. The cout.put will output a single character. 

    #include <iostream>
    using namespace std;
    int main()
    	char myStr[] = "This is a string of characters that is over 20 characters long";
    	char mych = 'X';
    	cout.write(myStr, 9);
    	cout << endl;
    	return 0;

    When you run the code, the output will be:

    This is a

    Adapted from:
    "getline() function and character array" by AdityaRakhechaGeeks for Geeks is licensed under CC BY-SA 4.0

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

    • Was this article helpful?