HTML table with C++

Hi!
I'm trying to use C++ to print HTML code for a table to a file, then open it for viewing (in a web browser or something that can read HTML). Here's what I've written so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 
This program creates a file in HTML, writes a table to the file, and opens it for viewing

Notes:

*/
#include <iostream> 
#include <fstream>
#include <string>
#include <vector>
using namespace std;

// This class allows a 2-dimensional table expressed as
// a vectors of vector of strings to be transformed into
// HTML form.
class HTMLTable
{
private:
   vector<string> headers;
   vector<vector<string> > rows;
   // Helper method for writing an HTML row in a table
   void writeRow (ostream &out, string tag, vector<string> row);
public:
   // Set headers for the table columns
   void setHeaders (const vector<string> &headers)
   {
      this->headers = headers;
   }
   // Add rows to the table
   void addRow(const vector<string> &row)
   {
      rows.push_back (row);
   }
   // Write the table into HTML form onto an output stream
   friend ostream &operator << (ostream &out, HTMLTable htmlTable);
};

//************************************************************
// Writes a row of the table, using the given tag for the    *
// table data. The tag may be td for table data or th for    *
// table header.                                             *
//************************************************************
void HTMLTable::writeRow (ostream &out, string tag, 
                                       vector<string> row)
{
   out << "<tr>\n";
   for (unsigned int k = 0; k < headers.size (); k++)
   {
      out << "<" << tag << "> "
         << row[k] << " </" << tag << "> ";
   }
   out << "\n</tr>\n";
}

//******************************************************
// Overloaded stream output operator <<                *
//******************************************************
ostream & operator << (ostream &out, HTMLTable htmlTable)
{
   out << "<table border = \"1\">\n";
   
   // Write the headers
   htmlTable.writeRow(out, "th", htmlTable.headers);
   
   // Write the rows of the table
   for (unsigned int r = 0; r < htmlTable.rows.size(); r++)
   {
      htmlTable.writeRow(out, "td", htmlTable.rows[r]);
   }
   // Write end tag for table
   out << "</table>\n";
   return out;
}

int main()
{
   // Definition for table column headers   
   vector<string> headers {"Name", "Address", "Phone"};

   // Definitions for the first two rows of the table
   vector<string> person1 {"Mike Sane", "1215 Mills St", "630-728-1293"};
   vector<string> person2 {"Natasha Upenski", "513 Briarcliff Ln", "412-672-1004"};
   
   // Create the HTML table object and set its members
   HTMLTable hTable;
   hTable.setHeaders(headers);
   hTable.addRow(person1);
   hTable.addRow(person2);

   // Open a file and write the HTML code to the file
   ofstream outFile("c:\\temp\\table.html");
   outFile << hTable;
   outFile.close();

   // Write the same HTML code to the screen for ease of viewing
   cout << hTable;
   // Use the default browser to view generated HTML table
   system("c:\\temp\\table.html");

   return 0;
}


My issue is that when I try to compile it, I get these 3 errors:
htmltable.cc:79:26: error: expected ';' at end of declaration
   vector<string> headers {"Name", "Address", "Phone"};
                         ^
                         ;
htmltable.cc:82:26: error: expected ';' at end of declaration
   vector<string> person1 {"Mike Sane", "1215 Mills St", "630-728-1293"};
                         ^
                         ;
htmltable.cc:83:26: error: expected ';' at end of declaration
   vector<string> person2 {"Natasha Upenski", "513 Briarcliff Ln", "412-672-1004"};
                         ^
                         ;

I always thought that this was the proper way to initialize a vector of strings? Any advice you have would be appreciated.
Thanks!
max
Last edited on
Why are you posting this in the lounge?
Don't spam the lounge with your beginner programming questions!
Anyway, presumably you aren't compiling as C++11 or greater.
Last edited on
I figured since it used HTML it belonged in the lounge...but ok. No need to be rude about it, though.

Um, how do I delete the topic?
Last edited on
Topic archived. No new replies allowed.