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
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
std::cout lets you combine text and numbers into one line of output by chaining them together with the ____ operator.
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
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
Why do you use \t at the same spot in multiple consecutive lines?
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.
Output
10 questions · pass with 60% — just like in the app.
Which header must you #include to use std::cout?
The << operator is used to send values to std::cout.
std::endl inserts a newline and also forces the output buffer to ____.
What does this code output?
std::cout << "A "; std::cout << "test" << std::endl;
\t produces a tab that helps align text into columns.
To output a quotation mark directly inside a string, you write ____.
Why do you have to write \\ to output a single backslash?
std::cout << "A\nB"; produces the same visible output as writing "A" and "B" with a newline between them using two statements.
What does the << operator let you do that is especially convenient in C++?
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 →