1. Intro
Formatted output refers to controlling how data appears on the console — its width, alignment, number of decimal places, and padding — rather than printing raw values. C++ achieves this using stream manipulators from <iomanip> combined with stream format flags.
Cricket analogy: A scoreboard displaying '045*' with padded zeros controls exactly how the run count is presented, just as C++ formatted output controls width and padding rather than the raw number.
2. Syntax
cout << left << setw(15) << name << right << setw(8) << score << endl;3. Explanation
By default, numeric output is right-aligned within its field width using setw, while left or right manipulators explicitly control alignment for text or numbers. fixed combined with setprecision(n) guarantees a consistent number of decimal places, which is essential for tables of prices or measurements. Formatted output is commonly used to build aligned tables in console applications, such as reports listing names alongside numeric scores or prices.
Cricket analogy: A scorecard right-aligns each batsman's run total within a fixed-width column using padding, just as setw right-aligns numeric output by default in C++.
When printing a table, remember that setw() only affects the next single field — reapply it before every column value in each row for consistent alignment.
4. Example
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << left << setw(10) << "Item" << right << setw(8) << "Price" << endl;
cout << left << setw(10) << "Apple" << right << setw(8) << fixed << setprecision(2) << 1.5 << endl;
cout << left << setw(10) << "Bread" << right << setw(8) << fixed << setprecision(2) << 2.75 << endl;
return 0;
}5. Output
Item Price
Apple 1.50
Bread 2.756. Key Takeaways
- Formatted output controls width, alignment, and precision instead of printing raw values.
leftandrightmanipulators set text alignment within a field.fixedwithsetprecision(n)ensures consistent decimal-place formatting.setw()must be reapplied before each field, including in table rows, since it does not persist.
Practice what you learned
1. What is the default alignment for numeric output in a field set by `setw`?
2. Which manipulator explicitly left-aligns output within its field?
3. What combination ensures a value always shows exactly 2 decimal places?
4. Why must `setw()` be reapplied for every column in a table?
5. What C++ feature is essential for building aligned console tables?
Was this page helpful?
You May Also Like
I/O Manipulators in C++
Explore C++ I/O manipulators from `<iomanip>` like `setw`, `setprecision`, `setfill`, and `fixed` used to control stream formatting.
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.
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
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