Lab 1 Code

Code

C++ code to generate the table.html

#include <iostream> #include <fstream> #include <vector> #include <string> #include <filesystem> namespace fs = std::filesystem; int main() { int numRows, numCols; std::string userDesktopPath = getenv("USERPROFILE"); userDesktopPath += "\\Desktop\\HtmlTable"; fs::create_directory(userDesktopPath); std::string fileLocation = userDesktopPath + "\\table.html"; std::cout << "Please enter the number of columns: "; std::cin >> numCols; std::cout << "Please enter the number of rows: "; std::cin >> numRows; std::vector<std::vector<std::string>> dataTable(numRows, std::vector<std::string>(numCols)); for (int row = 0; row < numRows; ++row) { for (int col = 0; col < numCols; ++col) { std::cout << "Enter data for cell (" << row + 1 << ", " << col + 1 << "): "; std::cin >> dataTable[row][col]; } } std::ofstream htmlFile(fileLocation); if (!htmlFile) { std::cerr << "Error creating the HTML file!" << std::endl; return 1; } htmlFile << "<!DOCTYPE html>\n<html>\n<head>\n<title>Table Data</title>\n<style>\n" << "table {border-collapse: collapse; width: 50%;} \nth, td {border: 1px solid black; padding: 8px;}"\n" << "th {background-color: lightgray;}"\n << "</style>\n</head>\n<body>\n" << "<h2>Generated Table</h2>\n<table>\n<tr>" for (int col = 0; col < numCols; ++col) { htmlFile << "<th>Column " << col + 1 << "</th>"; } htmlFile << "</tr>\n" for (int row = 0; row < numRows; ++row) { htmlFile << "<tr>" for (int col = 0; col < numCols; ++col) { htmlFile << "<td>" << dataTable[row][col] << "</td>"; } htmlFile << "</tr>\n"; } htmlFile << "</table>\n</body>\n</html>"; htmlFile.close(); std::cout << "The HTML table has been successfully created and saved at: " << fileLocation << std::endl; return 0; }
Web hosting by Somee.com