C++: Create an HTML Table

Bjarne-stroustrup
 

Create an HTML table.

  • The table body should have at least three rows of three columns.
  • Each of these three columns should be labelled “X”, “Y”, and “Z”.
  • An extra column should be added at either the extreme left or the extreme right of the table that has no heading, but is filled with sequential row numbers.
  • The rows of the “X”, “Y”, and “Z” columns should be filled with random or sequential integers having 4 digits or less.
  • The numbers should be aligned in the same fashion for all columns.

#include <fstream>
#include <boost/array.hpp>
#include <string>
#include <cstdlib>
#include <ctime>
#include <sstream>

void makeGap( int gap , std::string & text ) {
	for ( int i = 0 ; i < gap ; i++ ) 
	text.append( " " ) ;
}

int main( ) {
	boost::array<char , 3> chars = { 'X' , 'Y' , 'Z' } ;
	int headgap = 3 ;
	int bodygap = 3 ;
	int tablegap = 6 ;
	int rowgap = 9 ;
	std::string tabletext( "<html>\n" ) ;
	makeGap( headgap , tabletext ) ;
	tabletext += "<head></head>\n" ;
	makeGap( bodygap , tabletext ) ;
	tabletext += "<body>\n" ;
	makeGap( tablegap , tabletext ) ;
	tabletext += "<table>\n" ;
	makeGap( tablegap + 1 , tabletext ) ;
	tabletext += "<thead align=\"right\">\n" ;
	makeGap( tablegap, tabletext ) ;
	tabletext += "<tr><th></th>" ;
	for ( int i = 0 ; i < 3 ; i++ ) {
		tabletext += "<td>" ;
		tabletext += *(chars.begin( ) + i ) ;
		tabletext += "</td>" ;
	}
	tabletext += "</tr>\n" ;
	makeGap( tablegap + 1 , tabletext ) ;
	tabletext += "</thead>" ;
	makeGap( tablegap + 1 , tabletext ) ;
	tabletext += "<tbody align=\"right\">\n" ;
	srand( time( 0 ) ) ;
	for ( int row = 0 ; row < 5 ; row++ ) {
		makeGap( rowgap , tabletext ) ;
		std::ostringstream oss ;
		tabletext += "<tr><td>" ;
		oss << row ;
		tabletext += oss.str( ) ;
		for ( int col = 0 ; col < 3 ; col++ ) {
			oss.str( "" ) ;
			int randnumber = rand( ) % 10000 ;
			oss << randnumber ;
			tabletext += "<td>" ;
			tabletext.append( oss.str( ) ) ;
			tabletext += "</td>" ;
		}
		tabletext += "</tr>\n" ;
	}
	makeGap( tablegap + 1 , tabletext ) ;
	tabletext += "</tbody>\n" ;
	makeGap( tablegap , tabletext ) ;
	tabletext += "</table>\n" ;
	makeGap( bodygap , tabletext ) ;
	tabletext += "</body>\n" ;
	tabletext += "</html>\n" ;
	std::ofstream htmltable( "testtable.html" , std::ios::out | std::ios::trunc ) ;
	htmltable << tabletext ;
	htmltable.close( ) ;
	return 0 ;
}

Output ( of testtable.html ):

<html>
   <head></head>
   <body>
      <table>
       <thead align="right">
      <tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>
       </thead>
       <tbody align="right">
         <tr><td>0<td>1274</td><td>6847</td><td>352</td></tr>
         <tr><td>1<td>846</td><td>6577</td><td>4612</td></tr>
         <tr><td>2<td>7543</td><td>1644</td><td>8143</td></tr>
         <tr><td>3<td>4928</td><td>5714</td><td>8186</td></tr>
         <tr><td>4<td>3436</td><td>7493</td><td>9344</td></tr>
       </tbody>
      </table>
   </body>
</html>

SOURCE

Content is available under GNU Free Documentation License 1.2.