← Samples

Write data in columns

123456789101112#include <iostream> #include <iomanip> int main() { std::cout << std::left << std::setw(12) << "John Smith" << std::right << std::setw(3) << 23 << '\n'; std::cout << std::left << std::setw(12) << "Sam Brown" << std::right << std::setw(3) << 8 << '\n'; }

This sample is licensed under the CC0 Public Domain Dedication.

Intent

Align data in columns when writing to an output stream.

Description

On lines 6–11, we write two lines of data to an output stream. std::cout is used as the example stream.

We use I/O manipulators to align the data in columns. The std::setw manipulator sets the width of a column, while std::left and std::right set the alignment of the written value within that column. For example, on line 6, we write the name “John Smith” to a column of width 12 and align it to the left of the column.

Contributors

  • Joseph Mansfield

Last Updated

03 April 2015

Source

Fork this sample on GitHub

Share