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