C++ program to print data in a table

Introduction :

In this C++ tutorial, we will learn how to print different data in a table in C++. Basically, we will write one program that will print one table using a set of data. With this program, you will learn how to do input/output manipulation in C++. To print one table, we need to print the columns of equal width. Also, if the value of a column is less than the size of the column, it should add paddings to the value to make the width equal to other columns. We will use iomanip library with this program.

setfill() and setw() methods of iomanip :

C++ input/output manipulators library iomanip comes with a lot of useful methods. In this example, we are going to use setfill() and setw(). setw() is used to change the width of the output field. For example, if we are assigning the width as 4 and printing the word ‘one’ which is a three letter word, the output will add one blank space  to the result to make it equal to the width. setfill takes one character as the argument and uses it as the fill character. Let’s consider the below example :

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout
        << setfill('*')
        << setw(5)
        << "one" << endl;

    return 0;
}

It will print :

**one

We have set the width as 5 and printing the word ‘one’. It adds two more fill character ** to the left.

std::left and std::right :

By default, the printing word is added to the right side but we can also use std::left to print it on left. For example :

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout
        << setfill('*')
        << left
        << setw(5)
        << "one" << endl;

    return 0;
}

It will print :

one**

Example program with object class :

Let’s take a look at the below program :

#include <iostream>
#include <iomanip>

using namespace std;

class Student
{
public:
    string studentName;
    int studentAge;
    int studentMarks;
    int admissionYear;

    Student(string name, int age, int marks, int year)
    {
        studentName = name;
        studentAge = age;
        studentMarks = marks;
        admissionYear = year;
    }
};

int main()
{
    Student studentArray[4] = {Student("Alex", 20, 80, 2018), Student("Bob", 21, 82, 2018), Student("Chandler", 23, 85, 2017), Student("Rose", 18, 89, 2019)};

    cout
        << left
        << setw(10)
        << "Name"
        << left
        << setw(5)
        << "Age"
        << left
        << setw(8)
        << "Marks"
        << left
        << setw(5)
        << "Year"
        << endl;

    for (int i = 0; i < 4; i++)
    {
        cout
            << left
            << setw(10)
            << studentArray[i].studentName
            << left
            << setw(5)
            << studentArray[i].studentAge
            << left
            << setw(8)
            << studentArray[i].studentMarks
            << left
            << setw(5)
            << studentArray[i].admissionYear
            << endl;
    }
    return 0;
}

It will print the below output :

Name      Age  Marks   Year
Alex      20   80      2018
Bob       21   82      2018
Chandler  23   85      2017
Rose      18   89      2019

We have set different width for each columns. The first column width is 10, second column is 5, third column is 8 and last column is 5. The width is important here. If it is less than the size of its content, it the content will overflow. For example :

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout
        << left
        << setw(1)
        << "one" << endl;

    return 0;
}

It will print one even though we set the width as 1.

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊