×

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

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 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.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 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.

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.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

How to calculate size of string in C++

What is string in C++? In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the...

4 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.