← Back to C++ Course | Chapter 2: Input & Output | Lesson 7 of 8

C++ iomanip Library

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

cpp
#include <iostream>
#include <iomanip>

int main() {
	std::cout << std::setw(10) << "Name" << std::setw(10) << "Score" << std::endl;
	return 0;
}

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

cpp
#include <iostream>
#include <iomanip>

int main() {
	std::cout << std::setfill('0') << std::setw(5) << 42 << std::endl;
	return 0;
}

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

cpp
#include <iostream>
#include <iomanip>

int main() {
	double average = 3.14159265358979;
	std::cout << std::setprecision(4) << average << std::endl;
	return 0;
}

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

cpp
#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;
}

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

cpp
#include <iostream>
#include <iomanip>

int main() {
	double price = 19.99;
	std::cout << "$" << std::fixed << std::setprecision(2) << price << std::endl;
	return 0;
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.