1. Intro
getline() is a function in the <string> header used to read an entire line of text from an input stream, including embedded spaces, until a newline character or a specified delimiter is encountered. It solves the limitation of cin >>, which stops reading at the first whitespace.
Cricket analogy: A commentator reading out a player's full nickname 'Mahi the Captain Cool' word-for-word, spaces included, is like getline() capturing an entire line, unlike cin >> which would stop at the first space.
2. Syntax
getline(cin, str); // reads until newline
getline(cin, str, delimiter); // reads until custom delimiter3. Explanation
getline(cin, str) reads characters from cin into the string str until it encounters \n, which it consumes but does not store. An overloaded version, getline(cin, str, delim), reads until a custom delimiter character instead of the default newline. getline is essential whenever full sentences, names with spaces, or CSV-like fields need to be captured intact.
Cricket analogy: Reading a full over-by-over commentary line until the newline (which is consumed, not stored) is like getline(cin, str), while stopping instead at a comma to separate 'runs, wickets' fields is like the delimiter overload.
A classic gotcha: if cin >> var; is used before getline(cin, str);, the leftover newline character from the previous input stays in the buffer and is immediately consumed by getline, producing an empty string. Fix this by calling cin.ignore() after cin >> and before getline().
4. Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
string fullName;
cout << "Enter age: ";
cin >> age;
cin.ignore(); // discard leftover newline
cout << "Enter full name: ";
getline(cin, fullName);
cout << "Name: " << fullName << ", Age: " << age << endl;
return 0;
}5. Output
Enter age: 25
Enter full name: Maria Garcia
Name: Maria Garcia, Age: 256. Key Takeaways
getline(cin, str)reads an entire line, including spaces, unlikecin >> str.- The newline delimiter is consumed but not stored in the resulting string.
- A custom delimiter can be supplied via
getline(cin, str, delim). - Mixing
cin >>andgetline()requirescin.ignore()to discard a leftover newline.
Practice what you learned
1. What does `getline(cin, str)` read by default?
2. Is the newline delimiter stored inside the resulting string?
3. What issue occurs when `getline()` is called right after `cin >> var;`?
4. How can you fix the leftover-newline issue before calling `getline()`?
5. Which header must be included to use `getline` for `std::string`?
Was this page helpful?
You May Also Like
cin and cout in C++
Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.
Unformatted I/O Functions in C++
Learn C++ unformatted input/output functions like `cin.get()`, `cin.put()`, and `cin.ignore()` for low-level, character-based stream handling.
Strings in C++
Learn how the C++ `std::string` class represents text, why it is safer and more convenient than C-style char arrays, and how to declare and use it.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics