C/C++ Arena

Step 2 of 6

Formatting tables with iomanip

<iomanip> controls how values are printed:

Manipulator Effect
std::setw(n) pad the next item to width n (only the next one!)
std::left, std::right alignment inside that width (sticky)
std::fixed + std::setprecision(2) always 2 digits after the decimal point (sticky)
std::setfill('0') pad with a character other than space (sticky)

"Sticky" settings stay on the stream until you change them. setw resets after each item.

std::cout << std::left << std::setw(8) << "ropz" << std::right << std::setw(6) << 1.25 << "\n";

Your turn: read name kills deaths rows and print a table: name left-aligned in 10 columns, kills and deaths right-aligned in 4 columns each, then the K/D ratio right-aligned in 6 columns with 2 decimals (use 0 deaths as 1 death).

Previous: Parsing with istringstream Next: Building strings with ostringstream