C++ table alignment – cout and iomanip

I have a small alignment issue in my program.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << setw(5) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name" << endl << endl;
    cout << setw(5) << 1 << setw(15) << "January" << setw(15) << "Abhilash" << endl;
    cout << setw(5) << 2 << setw(15) << "Februaury" << setw(15) << "Anandan" << endl;
    cout << setw(5) << 3 << setw(15) << "March" << setw(15) << "Abhilash" << endl;
    cout << setw(5) << 4 << setw(15) << "April" << setw(15) << "Anandan" << endl;

    return 0;
}

In the output I get, the names of the months are not right justified.

Sl. No:          Month           Name

    1        January       Abhilash
    2      Februaury        Anandan
    3          March       Abhilash
    4          April        Anandan

What seems to be the problem?

Leave a Comment