Create a Table in C++

Aditya Raj Oct 12, 2023
  1. Functions to Create a Table in C++
  2. Create a Table With Proper Alignment in C++
  3. Conclusion
Create a Table in C++

In C++, the data can be displayed in the form of a table using the iomanip library. We will also learn about various methods of the iomanip library that helps to print data in the table.

In this article, we’ll discuss how to print data in the form of a table in C++.

Functions to Create a Table in C++

In C++, to print data in the table, we need to print the columns of equal width and use the iomanip library. If the value in any column is less than the width of the column, we need to add padding to make the width equal to other columns.

iomanip library provides many methods to manipulate the program’s output. We will only learn about the setfill() function and the setw() function as they are only required to print data in a table.

The setw() function is used to set the width of the output field. Syntax of the setw() function is written as setw(number), where number denotes the number of characters to be set in the width of the output field.

Example Code:

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

int main() {
  cout << setw(10) << "Monday" << endl;
  cout << setw(10) << "Tuesday" << endl;
  cout << setw(10) << "Wednesday" << endl;
  return 0;
}

Output:

    Monday
   Tuesday
 Wednesday

We have assigned the width as 10 to the output values while printing the words Monday, Tuesday, and Wednesday.

In the case of Monday, the output will add 4 blank spaces to make the width equal to 10, 3 blank spaces are added for Tuesday, and in the case of Wednesday, one blank space is added.

The setfill() function is used as a fill character. The syntax of the setfill() function is written as setfill(char ch), where ch is the character that needs to fill in the black spaces.

Example Code:

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

int main() {
  cout << setfill('#') << setw(10) << "Monday" << endl;
  cout << setfill('#') << setw(10) << "Tuesday" << endl;
  cout << setfill('#') << setw(10) << "Wednesday" << endl;
  return 0;
}

Output:

####Monday
###Tuesday
#Wednesday

We have assigned the width as 10 and printed the words Monday, Tuesday, and Wednesday. Without the setfill() function, the output would have contained space characters to make the output length of the words 10, as shown in the previous example.

However, when we use the setfill() method with the character # as its input argument, the spaces in the output are replaced by the character #.

For Monday, 4 fill characters # are used to make the width equal to 10. In the case of Tuesday, 3 fill characters are used, and for Wednesday, 1 fill character is used.

The fill character is placed on the left side by default, but we can also use std::left to print the fill character on the right side. You can observe this in the following example, where the fill character is printed on the right side of the output value.

Example Code:

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

int main() {
  cout << setfill('#') << left << setw(10) << "Monday" << endl;
  cout << setfill('#') << left << setw(10) << "Tuesday" << endl;
  cout << setfill('#') << left << setw(10) << "Wednesday" << endl;
  return 0;
}

Output:

Monday####
Tuesday###
Wednesday#

Create a Table With Proper Alignment in C++

Let’s look at an example to make a table in C++ with proper alignment. In the below example, we have printed the Age and Birthday in the table in C++.

We have used the setw() function of the iomanip library in C++ to set the width of the entries.

Example Code:

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

class Birthday {
 public:
  string studentName;
  int studentAge;
  int birthDay;
  string birthMonth;
  string birthYear;

  Birthday(string name, int age, int day, string month, string year) {
    studentName = name;
    studentAge = age;
    birthDay = day;
    birthMonth = month;
    birthYear = year;
  }
};

int main() {
  Birthday recordArray[5] = {Birthday("Naman", 20, 13, "January", "1999"),
                             Birthday("Divesh", 23, 04, "May", "1996"),
                             Birthday("Nitish", 19, 29, "December", "2000"),
                             Birthday("Mandeep", 25, 19, "March", "1998"),
                             Birthday("Aman", 20, 29, "April", "1989")};

  cout << left << setw(10) << "Name" << left << setw(5) << "Age" << left
       << setw(8) << "Day" << left << setw(10) << "Month" << left << setw(4)
       << "Year" << endl;

  for (int i = 0; i <= 4; i++) {
    cout << left << setw(10) << recordArray[i].studentName << left << setw(5)
         << recordArray[i].studentAge << left << setw(8)
         << recordArray[i].birthDay << left << setw(10)
         << recordArray[i].birthMonth << left << setw(4)
         << recordArray[i].birthYear << endl;
  }
  return 0;
}

Output:

Name      Age  Day     Month     Year
Naman     20   13      January   1999
Divesh    23   4       May       1996
Nitish    19   29      December  2000
Mandeep   25   19      March     1998
Aman      20   29      April     1989

We have set different widths in each column. The width of each column is more than the width of the content.

If the width of the output is less than the width of the column, then the content will overflow. Consider the below example.

Example Code:

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

int main() {
  cout << left << setw(1) << "January" << endl;
  return 0;
}

Output:

January

The output is printed to the table even if the width size is set to a number less than the length of the output. In this situation, the output length will be equal to the length of the output value.

Conclusion

We’ve discussed making a table using the iomanip library in C++. We learnt about the syntax of the setw() function and the setfill() function of the iomanip library.

We also discussed using these functions to make a table in C++. In the example, we have only used the setw() method to decide the column width and the setfill() function to format the table with different characters.

Related Article - C++ Table