Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 542
Mark Avatar asked Feb 24 '23 00:02

Mark


1 Answers

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';
}
like image 198
Cubbi Avatar answered Mar 05 '23 05:03

Cubbi