Parsing with
getline & stringstream
Learn how to read entire lines of input and break them into tokens using two essential C++ tools for text processing.
What is Parsing?
Parsing means taking a raw string of text and breaking it into meaningful pieces. Think of a comma-separated line like "Alice,95,A": parsing is how you extract the name, the score, and the grade as separate values your program can use.
Meet getline
getline(stream, string) reads everything from the current position up to a newline (or a custom delimiter) and stores it in a string. It consumes the delimiter but does not store it.
#include <iostream>
string line;
getline(cin, line);
// reads the entire line, including spaces
Meet stringstream
stringstream turns a plain string into a stream object, so you can use stream operators like >> and getline to extract data from it, just as you would from cin.
string data = "42 hello 3.14";
stringstream ss(data);
int n; string word; double d;
ss >> n >> word >> d;
// n=42, word="hello", d=3.14
The Two-Step Pattern
Core Syntax at a Glance
#include <string>
#include <sstream>
string line, token;
getline(cin, line); // Step 1: read full line
stringstream ss(line); // Step 2: wrap in stream
while (getline(ss, token, ',')) {
cout << token << endl;
}
getline vs cin >>
| getline | cin >> | |
|---|---|---|
| Reads until | newline (or custom delimiter) | whitespace |
| Keeps spaces? | Yes | No |
| Consumes delimiter? | Yes (discards it) | No (leaves it in stream) |
| Return type | Reference to the stream | Reference to the stream |
| Best for | Full lines, CSV data | Single words, numbers |
Step-by-Step Approach
<string>, <sstream>, and <iostream>.getline(cin, line) to capture everything, including spaces.stringstream ss(line) to turn the string into a parseable stream.getline(ss, token, delimiter) in a loop, or ss >> variable for typed extraction.stoi(), stod(), or use directly as a string.Example 1: Split by Comma
Task: Given the input "red,green,blue", print each color on its own line.
stringstream ss(line);
string color;
while (getline(ss, color, ',')) {
cout << color << endl;
}
Example 2: CSV with Types
Task: Parse "Alice,95,A" and store the name as a string, the score as an integer, and the grade as a character.
stringstream ss(line);
string name, scoreStr, gradeStr;
getline(ss, name, ',');
getline(ss, scoreStr, ',');
getline(ss, gradeStr);
int score = stoi(scoreStr);
char grade = gradeStr[0];
// name="Alice", score=95, grade='A'
Example 3: Mixed Extraction
Task: Read a line like "5 items cost 12.50" and extract the integer, string, string, and double using >> on a stringstream.
getline(cin, line);
stringstream ss(line);
int qty;
string w1, w2;
double price;
ss >> qty >> w1 >> w2 >> price;
// qty=5, w1="items", w2="cost", price=12.5
The cin >> then getline Trap
One of the most common bugs in C++ input: mixing cin >> with getline. After cin >> n, a leftover '\n' stays in the buffer. The next getline reads that newline as an empty string!
cin >> n; // reads "42", leaves '\n' behind
string line;
getline(cin, line); // reads "" (empty!)
When to Use Which Tool
Common Mistakes
Quick Reference Table
| Function | Syntax | Purpose |
|---|---|---|
| getline (newline) | getline(cin, str) |
Read a full line |
| getline (custom) | getline(ss, str, ',') |
Split by delimiter |
| stringstream | stringstream ss(line) |
Create stream from string |
| ss >> | ss >> var |
Extract typed values |
| stoi / stod | stoi(str) |
Convert string to number |
| cin.ignore() | cin.ignore() |
Clear leftover newline |
Question 1 of 5
Which header file must you include to use stringstream?
Question 2 of 5
What does getline(cin, line) do when it encounters a space character in the input?
Question 3 of 5
Given this code, what is the value of token after the first call to getline?
stringstream ss(s);
string token;
getline(ss, token, ':');
Question 4 of 5
A student writes cin >> n; followed by getline(cin, line);. The getline reads an empty string. What is the best fix?
Question 5 of 5
What is the output of the following code?
stringstream ss(data);
int x;
string word;
ss >> x >> word;
cout << x << " " << word;