C++ iomanip Library
In this page:
Setting Width with setw
setw(n) reserves a fixed-width field of n characters for the very next value printed, padding it with spaces so columns of output line up neatly. It only affects the single next output operation, so you need to call it again before each value if you want a whole table of aligned columns.
Example: Setting Width with setw
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setw(10) << "Name" << std::setw(10) << "Score" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Setting Fill Characters with setfill
setfill(c) changes what character fills the empty space setw leaves behind — spaces by default, but you might use 0 to zero-pad numbers or '*' for a visual separator. It stays in effect for all subsequent setw calls until you change it again, unlike setw which only applies once.
Example: Setting Fill Characters with setfill
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setfill('0') << std::setw(5) << 42 << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Floating Point Precision
setprecision(n) caps how many significant digits (or decimal places, when combined with fixed) are shown for floating-point output, which prevents long, messy strings of digits like 3.14159265358979. This is essential any time you're displaying computed values like averages or percentages to a user.
Example: Floating Point Precision
#include <iostream>
#include <iomanip>
int main() {
double average = 3.14159265358979;
std::cout << std::setprecision(4) << average << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left and Right Alignment
left and right control which side of a setw-defined field the text sticks to — left pushes text to the start with padding trailing after it, while right (the default) pads before the text so it aligns against the field's right edge. Switching between them is what lets you build both left-aligned labels and right-aligned numbers in the same table.
Example: Left and Right Alignment
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::left << std::setw(10) << "Name" << std::endl;
std::cout << std::right << std::setw(10) << "Score" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Fixed Point Notation
Combining fixed with setprecision(2) locks floating-point output to exactly two decimal places regardless of the actual value's precision, which is exactly the format expected for displaying currency like $19.99. Without fixed, setprecision alone controls total significant digits rather than decimal places specifically, which can produce unexpected results for values like 100.5.
Example: Fixed Point Notation
#include <iostream>
#include <iomanip>
int main() {
double price = 19.99;
std::cout << "$" << std::fixed << std::setprecision(2) << price << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: