Mark
Mark

Reputation: 8678

Align Integer using cout in C++

I need C++ to use cout to print:

Header
     1
     2
     3
     4
     5
    10
    11
    12

instead of

Header
    1
    2
    3
    4
    5
    10
    11
    12

How should I format this using cout?

Upvotes: 4

Views: 1725

Answers (1)

Cubbi
Cubbi

Reputation: 47428

use the IO manipulator setw

#include <iostream>
#include <iomanip>
int main()
{
    std::cout << "Header\n";
    for(int i=1; i<13; ++i)
        std::cout << std::setw(6) << i << '\n';
}

Upvotes: 8

Related Questions