×

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the program in one go, meaning it is not interpreted line by line but is a compiled language.

C++ programming language combines features of low-level programming language and high-level programming language (desktop apps, games, etc.); hence it is called mid-level programming language. It is a language that is machine independent but platform dependent, which means the program compiled on Linux need not be necessary to run in other operating systems like windows, mac, etc.

It has a rich set of libraries. The execution speed in the C++ programming language is high as compared to other programming languages like C and python. One of the best advantages of C++ programming language is that it separates from C programing language is object-oriented support which helps c++ language justifiable and extensible programs, which helps build various applications.

Creating a table in c++ programming language :

In c++ programming language, to form a table, we use a function called Romania library, which is used to form a table, and also data is displayed in the form of the table using this function. The function iomanip library helps the user to create a table in c++ programing language for printing the data in the form of a table or to create a table. First of all, we need to create columns of the same width and also use the library called iomanip library. We put on padding if any value in the column is lesser than that of column width to equalize the width of the column with the other columns. The functions setfill( ) function and setw( ) function are used to print the information or data in the the table. The function setw( ) sets the width of the output department. The syntax for this function is given by setw( number ); here, the word ‘ number ’ defines the number of characters that have to be set in the width. Now let us look into the code to understand the function setw( ).

Example code

#include <

#include < iostream >
#include < iomanip >
using namespace std ;
int main( )
{
    cout<< setfill( '*' ) << setw(10)<< " Friday " << endl ;
    cout<< setfill( '*' ) << setw(10)<< " Saturday " << endl ;
    cout<< setfill( '*' ) << setw(10)<< " Sunday " << endl ;
    return 0 ;
}

Output

Friday
Saturday
Sunday

Friday, Saturday, and Sunday. The above example, 10 widths are applied to the output values for printing the words. In the case of ‘ Friday ‘, the output adds 4 spaces to make the width the same as 10, and it adds 2 spaces to ‘ Saturday ’ to make the width equal to 10 and 4 spaces for the output word ‘ Sunday ‘.

The function setfill( ) is used to carry to fill the characters in blank spaces. The syntax for the function is given by  setfill (char); here, ch represents that the character is to be filled in the blank spaces.

Creating a table in c++

Example

#include <iostream>
#include <iomanip>
using namespace std;
class dateofbirth
{
  public:
    string nameofstudent;
    int ageofstudent;
    int dayofbirth;
    string monthofbirth;
    string yearofbirh=th;
datepfbirth(string name, int age, int day, string month, string year)
    {
nameofstudent = name;
        ageofstudent = age;
        dayofbirth = day;
        monthofbirrth = month;
        yearofbirth = year;
    }
};


int main()
{
    Birthday recordArray[5] = {Birthday("rohit", 27, 14, "January","2003"),                                                            Birthday("deeraj", 24, 05, "May","1997"),                                                Birthday("nikith", 18, 28, "December","2001"),                                            Birthday("Mandees", 26, 18, "March","1998"),
                               Birthday("Avan", 21, 28, "April","1988")};


    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].nameofstudent
             << left << setw(5) << recordArray[i].ageofstudent
             << left << setw(8) << recordArray[i].dayofbirth
             << left << setw(10) << recordArray[i].monthofbirth
             << left << setw(4) << recordArray[i].birthofyear
            << endl;
    }
    return 0;
}

Output

Name           Age      Day       Month         Year
rohit          27       14       January        1998
Deeraj         24       05       May            1997
Nikith         18       28       December       2001
Mandeeps       26       18       March          1999
Avan           21       28       April          1988

Related Topics

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.