Hello aaronpeart,
Sorry I could not do much before your deadline.
Just to help you in the future.
Your header files is missing <string> and I added <iomanip>.
Lines 7 - 12 should be inside main and you should avoid using global variables unless they start with "constexpr" or "const" as these can not be changed.
Your prototypes are good and I think I see their use.
After you open the input file you need to check that it is open and in good order. The output does not need any verification because for an output file if the file does not exist it creates the file.
The first bit of output code, I think this might be the "head" function, I used the "setw()" to space the tags. The "setw"s are not perfect and still need some adjustment.
Line 43. This is not the best way to read a file.
while(!inFile.eof())
I do not know why people teach this. This is a bad idea as it does not work the way you think it will. Generally by the time the while condition determines that "eof" has been reached you will have processed the last read twice and wondering why it shows up twice.
What happens is the while condition is checked and “eof” has not been set yet. After entering the while loop you read the last line or bit of information from the file process the information reach the bottom of the while loop. At this point “eof” has not been set yet and you return to the while condition which is still good. After entering the loop you try to read from the file, but now there is nothing left to read and “eof” is set, but you do nothing at this point to deal with the “eof” condition. So you process what information is in the variables from the last read before returning to the while condition where it finally detects the “eof”, but you have processed the last good read twice unless you have cleared the variables at the end of the while loop in which case the last process will be blank.
A more acceptable way of using the while loop is:
1 2 3 4 5
|
while (infile >> someVariable)
{
infile >> additionalVariables;
// Additional code.
}
| |
In the above example you can set up the while condition to read one variable, two or more variables or use “std::getline(?, str)” where ? could be from “std::cin” or from a file stream and “str” represents any “std::string” variable.
As you will in the while loop I added some "endl"s and for the last "getline" I removed the delimiter of "\t" because this is reading the end of the line which ends with "\n" not "\t". The default delimiter for "getline" is "\n". If I understood correctly this is reading the information into the proper variables.
After I adjusted the program I opened the output file in my browser and it made a nice table.
In the end it really does not make any difference how the output file is indented as long as the tags are correct.
For what it is worth when I did something like this I first started by writing the "html" code to get an idea of how to write the C++ code. In the end I divided up the "html" code into three sections. The first section included the lines "<DOCTYPE! HTML>" to the opening "<body>" tag. This mad it easy to read the input file and write it to the output file. The third section finished up the "html" code that would not change all the way down to the closing "html" tag.
For the second section this is what the program would put together based on reading a "csv" file that created most of the table.
After processing the second section and the doing the third section my program would finish with writing a bit of JavaScript at the end.
Next time do not wait until the end to ask a question. Give your self more time for the questions and answers.
This is the code I changed this morning. It is not perfect, but should be enough for ideas and what should be done. Be sure to read the comments I put in the code.
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
|
#include <iostream>
#include <iomanip> // <--- Added.
#include <string> // <--- Added.
#include <fstream>
//using namespace std; // <--- Best not to use.
std::string Cnumber = "Course Number";
std::string Cname = "Course Name";
std::string date1 = "Date2";
std::string date2 = "Date1";
std::string times = "Times";
std::string Dnumber = "Number of Days";
// I think I have an idea what these are used for and it is a good idea. I used files for this when I did
// my program.
void head();
void table();
void foot();
int main()
{
std::ifstream fin;
std::ofstream fout;
fin.open("Schedule.txt");
// <--- You should check that the input file is open.
fout.open("site.html");
fout << "<DOCTYPE! HTML>" << std::endl;
fout << "<html>" << std::endl;
fout << std::setw(4) << "<head>" << std::endl; // <--- The "setw()" makes it easier to keep track of the indenting.
fout << std::setw(4) << "</head>" << std::endl;
fout << std::setw(4) << "<body>" << std::endl;
fout << std::setw(8) << "<table border = \"2\" bordercolor = \"black\" bgcolor = \"lightblue\">" << std::endl; // <--- I believe the attributes should be in double quotes not single.
fout << std::setw(12) << std::endl;
fout << std::setw(12) << "<tr>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Course Number" << "</td>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Course Name" << "</td>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Start" << "</td>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Finish" << "</td>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Times" << "</td>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << "Number of Days" << "</td>" << std::endl;
fout << std::setw(12) << "</tr>" << std::endl;
while (std::getline(fin, Cnumber, '\t')) // <--- Better way than "eof".
{
fout << std::setw(12) << "<tr>" << std::endl;
fout << "\t" << "\t" << "\t" << "<td>" << Cnumber << "</td>" << std::endl; // <--- Added the "endl".
std::getline(fin, Cname, '\t');
fout << "\t" << "\t" << "\t" << "<td>" << Cname << "</td>" << std::endl; // <--- Added the "endl".
std::getline(fin, date1, '\t');
fout << "\t" << "\t" << "\t" << "<td>" << date1 << "</td>" << std::endl; // <--- Added the "endl".
std::getline(fin, date2, '\t');
fout << "\t" << "\t" << "\t" << "<td>" << date2 << "</td>" << std::endl; // <--- Added the "endl".
std::getline(fin, times, '\t');
fout << "\t" << "\t" << "\t" << "<td>" << times << "</td>" << std::endl; // <--- Added the "endl".
std::getline(fin, Dnumber); // <--- Removed the delimiter. The default is '\n' which you need here.
fout << "\t" << "\t" << "\t" << "<td>" << Dnumber << "</td>" << std::endl;
std::cout << std::endl;
}
fout << std::setw(12) << "</tr>" << std::endl;
fout << "\t" << "\t" << "</table>" << std::endl;
fout << "\t" << "</body>" << std::endl;
fout << "</html>" << std::endl;
return 0;
}
| |
Almost forgot. You are missing some closing "tr" tags. Although this is not absolutely necessary it is good form and programming to include them.
Hope that gives you some ideas for the future,
Andy