IAD Lab-1: C++ Task: Generate an HTML Table

Problem Statement:

Consider the table shown in Figure 1 and Code Listing 1. Try to develop a solution to the following problem:

Problem Statement:

Write code using C or C++ which performs the following tasks:

C++ Code Implementation

View My Report

Here is the code implemented by me in the above report


#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
int main() { 
 string data; 
 int columnnumber, rownumber; 
 cout<<"Enter Number of Rows "; 
 cin>>rownumber; 
 cout<<"Enter Number of Columns "; 
 cin>>columnnumber; 
 cin.ignore(); 
 ofstream file("index.html");  

 if (file.is_open()) {  
  file << "<!DOCTYPE html> \n<html> \n<head> \n<style> \ntable {font-family: arial, sans-serif; 
border-collapse: collapse; width: 100%; } \ntd, th
{ border: 1px solid #dddddd; text-align: left; padding: 8px; }\ntr:nth-child(even)
{ background-color: #dddddd; }\n</style>\n</head>\n<body>\n<h2>HTML Table</h2>\n<table>";
for (int i = 0; i < rownumber; i++) { file << "\n<tr>"; for (int j = 0; j < columnnumber; j++) { cout<<"Enter Your Data # [" << i+1 <<"][" <<j+1<<"] : "; getline(cin, data); if (i == 0) { file << "\n <th>" << data << "</th>"; } else { file << "\n <td>" << data << "</td>"; } } file << "\n</tr>"; } file << "\n</table>\n</body>\n</html> "; file.close(); cout << "\nData written successfully!\n"; } else { cout << "Error opening file!\n"; } return 0; }

HTML File Generated

<!DOCTYPE html> <html> <head> <style> table {font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head> <body> <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro Commercial Moctezuma</td> <td>Fransisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Ronald Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Hellen Bernett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> </body> </html>


Sample Outputs:

Live Demonstration: Lab 1 Live Page demonstration

Web hosting by Somee.com