01 / 20

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.

C++ Programming 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 <string>
#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.

#include <sstream>

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

Raw Input "Alice,95,A" getline string line "Alice,95,A" ss(line) stringstream ready to parse getline(ss,...,',') "Alice" "95" "A" Step 1: getline(cin, line) reads the full line Step 2: stringstream ss(line) + getline(ss, token, ',') splits by delimiter

Core Syntax at a Glance

#include <iostream>
#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

1
Include headers. You need <string>, <sstream>, and <iostream>.
2
Read the whole line. Call getline(cin, line) to capture everything, including spaces.
3
Wrap in stringstream. Create stringstream ss(line) to turn the string into a parseable stream.
4
Extract tokens. Use getline(ss, token, delimiter) in a loop, or ss >> variable for typed extraction.
5
Process each token. Convert with 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.

string line = "red,green,blue";
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.

string line = "Alice,95,A";
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.

string line;
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!

int n;
cin >> n; // reads "42", leaves '\n' behind

string line;
getline(cin, line); // reads "" (empty!)

When to Use Which Tool

getline alone
When you just need the whole line as a single string, like reading a sentence or paragraph of text.
ss >>
When data is space-separated and you know the types (int, double, string) in advance.
getline + ss
When data uses a non-space delimiter like commas, colons, or pipes: the classic CSV pattern.

Common Mistakes

✕ Forgetting cin.ignore()
Mixing cin >> before getline without clearing the leftover newline causes getline to read an empty string.
✕ Wrong header
Using stringstream without including <sstream> causes a compilation error: "stringstream not declared."
✕ Not converting types
getline always gives you a string. Forgetting stoi() or stod() means you are comparing or adding strings, not numbers.

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?

string s = "10:20:30";
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?

string data = "3 apples";
stringstream ss(data);
int x;
string word;
ss >> x >> word;
cout << x << " " << word;