C++ getline()
In C++, the getline() function converts user input into a character-delimited string and stores it into a variable. If a delimiting character is not specified, then the input is stored until \n (newline) is encountered. This function is widely used in reading multi-word user input, processing lines from text files, and parsing structured data such as CSV records or configuration files where input must be read line-by-line.
Syntax
The getline() function is defined in the <string> header:
getline(cin, string, delim)
Parameters:
cin: The object in the stream, which would be the user input.string: Refers to the variable to which the user input is set.delim(Optional): Refers to a character the user input is stored up to.
Although not recommended in C++, since it deals with C strings, getline() can be called on the cin object as a member function like this:
cin.getline(string, length, delim)
Parameters:
string: Refers to the variable to which the user input is set.length: The maximum number of characters to read, including the null terminator.delim(Optional): Refers to a character the user input is stored up to.
Example 1: Using getline() Without delim
This example demonstrates the usage of the getline() function without the delim parameter:
#include <iostream>#include <string>using namespace std;int main() {// Create a stringstring my_str;// Take input from the usercout << "Enter a pet name: ";// Store the input in the stringgetline(cin, my_str);cout << "My pet's name is " + my_str + "!";}
Here is the output if the user input is “Nimbus”:
Enter a pet name: NimbusMy pet's name is Nimbus!
Example 2: Using getline() with delim
This example demonstrates the usage of the getline() function with the delim parameter:
#include <iostream>#include <string>using namespace std;int main() {// Create a stringstring my_str;// Take input from the usercout << "Enter a pet name: ";// Store the input in the string until '\n' (newline) is encounteredgetline(cin, my_str, '\n');cout << "My pet's name is " + my_str + "!";}
Here is the output if the user input is “Dora”:
Enter a pet name: DoraMy pet's name is Dora!
Codebyte Example: Using getline() on cin
This codebyte example applies the getline() function on the cin object:
getline() vs cin
| Feature | getline() |
cin |
|---|---|---|
| Reads input until | Newline (\n) |
Whitespace (space, tab, newline) |
| Captures spaces | Yes | No |
| Input type | Entire line as std::string |
Word/token as std::string, int, etc. |
| Common use cases | Full names, sentences, file lines | Single words, numbers, basic input |
| Header required | <string> and <iostream> |
<iostream> |
| Works with file streams | Yes | Yes |
| Buffer issues with mixing | Must use cin.ignore() after cin |
Not affected when used alone |
| Ease of parsing line-by-line | Excellent | Limited |
Frequently Asked Questions
1. Can getline() read input from files?
Yes, getline() works with any input stream, including file streams (std::ifstream).
2. Does getline() include the newline character (\n) in the string?
No, getline() stops reading at the newline but does not store it in the string. The result is a clean line without the \n.
3. Can I use a custom delimiter with getline()?
Yes, you can specify a delimiter:
std::getline(std::cin, str, ';'); // Stops at semicolon instead of newline
This is useful for parsing CSV or semi-colon-separated input.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours