C++ getline()

In the C++ programming language, the getline() function is a versatile tool that allows you to read input from the user or from a file. It is particularly useful when you need to read a line of text, including spaces, until a specified delimiter is encountered.

The syntax for using the getline() function is as follows:

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

Let’s break down the parameters:

  • is: This is the input stream object from which the function will read the data. It can be cin for user input or an ifstream object for reading from a file.
  • str: This is the string object where the data will be stored.
  • delim: This is the delimiter character that indicates where the line should end. It is optional and defaults to the newline character ('n') if not specified.

Now, let’s explore some examples to better understand how the getline() function works:

Example 1: Reading User Input

#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::getline(std::cin, name);
    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

In this example, we prompt the user to enter their name. The getline() function reads the entire line of input until the newline character ('n') is encountered. The entered name is then stored in the name variable, and we output a personalized greeting.

Example 2: Reading Lines from a File

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("example.txt");
    std::string line;
    while (std::getline(file, line)) {
        std::cout << line << std::endl;
    }
    file.close();
    return 0;
}

In this example, we open a file named “example.txt” using an ifstream object. We then use a while loop to read each line from the file using getline(). The function reads the line until the newline character ('n') is encountered and stores it in the line variable. We then output each line to the console. Finally, we close the file once we have finished reading all the lines.

Example 3: Specifying a Custom Delimiter

#include <iostream>
#include <string>

int main() {
    std::string sentence;
    std::cout << "Enter a sentence (use '#' as the delimiter): ";
    std::getline(std::cin, sentence, '#');
    std::cout << "You entered: " << sentence << std::endl;
    return 0;
}

In this example, we ask the user to enter a sentence and specify the ‘#’ character as the delimiter. The getline() function reads the input until it encounters the ‘#’ character and stores it in the sentence variable. We then output the entered sentence.

These examples demonstrate the flexibility and usefulness of the getline() function in C++. Whether you need to read user input or lines from a file, this function provides a reliable way to handle input with ease.

Remember to include the necessary headers (#include <iostream>, #include <string>, #include <fstream>) depending on your specific use case.

Scroll to Top