public
anonymous / gist:5332564
Created

Here is the code I have for a certain project. The problem I am having mostly is concerned with the void function at the bottom of the code. It is a function that writes the body of an HTML document that can be accessed in a browser. Right now it consists of a table with 2 columns labeled "KEY" and "VALUE" respectively. The way the code has it, it will display the integers 1-10 under the KEY column and zeros under the VALUE column. However, I am asked to give 255 rows and 2 columns where each row is the integer representation of the ASCII character and its count. Can anyone help me get this right? Thank you for any help!

  • Download Gist
gistfile1.txt
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
 
#define ARRAY_SIZE 256
 
using namespace std;
 
void initializeArray(int nArray [], int nArraySize);
void outputCSV(int nArray [], int nArraySize, ofstream& fCSV);
void outputHTML(int nArray [], int nArraySize, ofstream& fHTML);
void writeHeader (ofstream& ofHtmlFile);
void writeBody (ofstream& ofHtmlFile, int nArray [], int nArraySize);
void writeFooter ( ofstream& ofHtmlFile);
 
int main (int argc, char *argv[] )
{
ifstream fin;
ofstream fHTML;
ofstream fCSV;
string sInputFileName="";
char cInput=' ';
int nInput=0;
int nArray[ARRAY_SIZE];
 
for (int i=0; i<argc; i++)
{
string sTemp="";
cout<<"argument " << i << "=" << argv[i] << endl;
sTemp=argv[i];
if (sTemp.compare("-f")==0)
{
sInputFileName=argv[i+1];
cout << "The input file name is " << sInputFileName << endl;
}
}
 
/*fin.open(sInputFileName);
if (fin.fail())
{ //check for sucesfful file opening for reading
cout << "Input file opening failed" << endl;
exit(1);
}*/
fHTML.open ("project4.html");
if (fHTML.fail())
{ //check for sucesfful file opening for writing
cout << "Failure opening output file project4.html" << endl;
exit(1);
}
fCSV.open("project4.csv");
if (fCSV.fail())
{ //check for sucesfful file opening for writing
cout << "Failure opening output file project4.csv" << endl;
exit(1);
}
 
initializeArray(nArray, ARRAY_SIZE); //initialize all members of the array to 0
while (!fin.eof()) //loop until we reach the end of the input file
{
char cInput=fin.get(); //get the character from the file
int nInput=(int)cInput; //convert to integer
if ((0 < nInput) && (nInput < ARRAY_SIZE))
{
++nArray[nInput]; //increment the counter
}
}
fin.close( ); //closes the input file
outputCSV(nArray, ARRAY_SIZE, fCSV);
outputHTML(nArray, ARRAY_SIZE, fHTML);
fHTML.close( ); //closes the HTML output file
fCSV.close( ); // closes the CSV output file
 
 
cin.get();
return 0;
}
 
void initializeArray(int nArray[], int nArraySize)
{
for (int i=0; i<nArraySize; i++) //loop through all members of the array
{
nArray [i]=0; // set the ith member to 0
}
return;
}
 
void outputCSV(int nArray[], int nArraySize, ofstream& fCSV)
{
for (int i=0; i<nArraySize; i++)
{
fCSV << i; //output the value of i
fCSV.put (','); //separates each value with a comma
}
fCSV.put('\n'); //end of the first line (the header record)
 
for (int i=0; i<nArraySize; i++)
{
fCSV << nArray[i];
fCSV.put(',');
}
fCSV.put('\n');
return;
}
 
void outputHTML(int nArray [], int nArraySize, ofstream& fHTML)
{
ofstream ofHtmlFile;
cout << "Start of program." << endl;
ofHtmlFile.open("project4.html");
if (ofHtmlFile.fail ())
{
cout << "Open project4.html failed." << endl;
exit (1);
}
writeHeader(ofHtmlFile);
writeBody(ofHtmlFile, nArray, nArraySize);
writeFooter(ofHtmlFile);
ofHtmlFile.close ();
cout << "End of program." << endl;
return;
}
 
void writeHeader( ofstream& ofHtmlFile)
{
ofHtmlFile << "<HTML>" << endl;
ofHtmlFile << "<HEAD>" << endl;
ofHtmlFile << "<TITLE>" << endl;
ofHtmlFile << "Kenny's array table" << endl;
ofHtmlFile << "</TITLE>" << endl;
ofHtmlFile << "Welcome to this computer generated webpage! <br>" << endl;
ofHtmlFile << "</HEAD>" << endl;
return;
}
 
void writeFooter( ofstream& ofHtmlFile)
{
ofHtmlFile << "</HTML>" << endl;
return;
}
 
void writeBody (ofstream& ofHtmlFile, int nArray[], int nArraySize)
{
 
ofHtmlFile << "<BODY>" << endl;
ofHtmlFile << "This is where you place your content." << endl;
ofHtmlFile << "<TABLE BORDER=\"1\">" << endl;
ofHtmlFile << "<th> KEY </th>" << endl;
ofHtmlFile << "<th> VALUE </th>" << endl;
for (int i=0; i<=10; i++)
{
ofHtmlFile << "<tr><td>" << i << "</td><td>" << nArray[i] << "</td></tr>"; //this seems to be the line with the problems.
}
ofHtmlFile << " </TABLE>" << endl;
ofHtmlFile << "</BODY>" << endl;
return;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.