Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Output · Lesson 16 of 25

Formatting Output

A free lesson from Swipe Coding · C++, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
You've basically got C++'s whole output toolkit now — let's put it all together. 🧰

One of the handiest things about std::cout is that you can chain multiple pieces together in a single statement, one after another, each separated by its own <<. This works for text, numbers, and other values all mixed together — no matter the type, each one just gets sent to the console in order.

int quantity = 3;
double price = 4.5;
std::cout << "You bought " << quantity << " items for $" << price << std::endl;

// Output:
// You bought 3 items for $4.5
A single chained statement mixes a string, an int, another string, and a double — all in one line.
Fill in the blank

std::cout lets you combine text and numbers into one line of output by chaining them together with the ____ operator.

🤖
One statement can replace four separate ones. ⚡

You can also combine \n with chaining to print several lines from a single statement, instead of writing a separate std::cout line for each one.

std::cout << "=== Shopping List ===\n" << "Apples\n" << "Bread\n" << "Milk" << std::endl;

// Output:
// === Shopping List ===
// Apples
// Bread
// Milk
A single chained statement with \n produces the exact same output as four separate std::cout statements.
True or false

std::cout << "A\nB"; produces the same output as writing std::cout << "A" << std::endl; followed by std::cout << "B";.

With \t you can make multiple lines look like a small table by aligning text into columns.

std::cout << "Product\tPrice\n";
std::cout << "Apple\t$0.50\n";
std::cout << "Bread\t$2.00\n";

// Output:
// Product  Price
// Apple    $0.50
// Bread    $2.00
Each line begins with a word followed by \t — this makes the prices roughly line up into a column.
Quick check

Why do you use \t at the same spot in multiple consecutive lines?

🤖
No + needed here — cout just keeps taking more pieces. 🧩

Notice that, unlike some languages, you did not need any special conversion to print quantity or price alongside plain text — std::cout already knows how to turn an int or a double into readable text. You will meet a different way to join text together, using the + operator on std::string, in an upcoming lesson.

Chapter quiz

Output

10 questions · pass with 60% — just like in the app.

Quick check

Which header must you #include to use std::cout?

True or false

The << operator is used to send values to std::cout.

Fill in the blank

std::endl inserts a newline and also forces the output buffer to ____.

Quick check

What does this code output?

std::cout << "A ";
std::cout << "test" << std::endl;
True or false

\t produces a tab that helps align text into columns.

Fill in the blank

To output a quotation mark directly inside a string, you write ____.

Quick check

Why do you have to write \\ to output a single backslash?

True or false

std::cout << "A\nB"; produces the same visible output as writing "A" and "B" with a newline between them using two statements.

Quick check

What does the << operator let you do that is especially convenient in C++?

Fill in the blank

Using namespace std; is ____ before you can use std::cout, since the std:: prefix already identifies it.

Keep your streak going in Swipe Coding

Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →