introduce c++ into html

79 Views Asked by At

So I need to create a data base of a student and his marks in class. I did it by using massive Arrays of objects, but I need to write it in html file like a table, and I create special function save.

#include <iostream>
#include "windows.h"
#include <fstream>
#include "TkachenkoLab.h"
using namespace std;

void save(Student KI[]);
ofstream file_out("C:\\Users\\ТКаченко\\Desktop\\МП ЛАБ №7\\LAB7\\page.html");
int main()
{
SetConsoleCP(::GetACP());
SetConsoleOutputCP(::GetACP());
short n;

cout << "Ââåä³òü ê³ëüê³ñòü ñòóäåíò³â ãðóïè: ";
cin >> n;
cin.clear(); cin.sync();
cout << "\n —-— ÑÒÂÎÐÅÍÍß ÃÐÓÏÈ Ê² —---\n";
Student KI[n];

cout << "\n —-— ÑÏÈÑÎÊ ÃÐÓÏÈ Ê² —---\n";
short i;
for (i = 0; i < Student::cnt(); i++ )
cout << i+1 << ". " << KI[i].name << endl;

cout << "\n —-— reading student —-— \n";
for (i = 0; i < Student::cnt(); i++ )
KI[i].in_res();

save(KI);
if (file_out.is_open())
    file_out.close();

return 0;
}
void save(Student KI[])
{
    file_out.open("C:\\Users\\ТКаченко\\Desktop\\МП ЛАБ №7\\LAB7\\page.html",ios::trunc);
    file_out << "<html>" << endl;
    file_out << "<head>" << endl;
    file_out << "</head>" << endl;
    file_out << "<body>" << endl;
    file_out << "<table class=\"simple-little-table\">" << endl;
    file_out << "<tr>" << endl;
    file_out << "<td>студент</td>" << endl;
    file_out << "<td>мп</td>" << endl;
    file_out << "<td>кс</td>" << endl;
    file_out << "<td>физра</td>" << endl;
    file_out << "<td>средний бал</td>" << endl;
    file_out << "</tr>" << endl;
    for (short i = 0; i < Student::cnt(); i++ )
    {
        file_out << "<td>студент</td>" << endl;
        file_out << "<td>"<<KI[i].name<<"</td>" << endl;
        file_out << "<td>"<<KI[i].MP<<"</td>" << endl;
        file_out << "<td>"<<KI[i].KC<<"</td>" << endl;
        file_out << "<td>"<<KI[i].fiz_ra<<"</td>" << endl;
        file_out << "</tr>" << endl;

    }
    file_out << "</table>" << endl;
    file_out << "</body>" << endl;
    file_out << "</html>" << endl;
    file_out.close();
}

my class in library

#include <string>
using namespace std;

class Student
{
public:
Student();
static short cnt() { return cnt_stud; };
void in_res();
void out_res();
string name;
~Student() {};
short MP, KC, fiz_ra;
static short cnt_stud;
short s_bal () { return (short)(MP+KC+fiz_ra)/3; };

};
void Student::out_res()
{
}

Student::Student()
{
cout << "ПІБ студента: ";
getline(cin, name);
MP = 0;
KC = 0;
fiz_ra = 0;
cnt_stud++;
};

short Student::cnt_stud = 0;

void Student::in_res()
{
cout << name << ": ";
cin >> MP; cin >> KC; cin >> fiz_ra;
}

It is didn't even create a file. What did I do wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

First you're using non-ascii characters in your pathname. I've never tried to do it so I can't help you there but maybe you can try the answer here Open File with Non ASCII Characters

You're doing it wrong, the way you go about writing to the file. I assume that you're trying to write all students into the same file, but don't make your file a global variable, pass it to the function like this.

void save( std::ofstream &file, Student KI[] );

And check things in main like this,

int main() {
    std::ofstream file( "PathOfFile" );
    if ( file.is_open() ) {
        // Do what you want to do with your file
        save( file, student );

        file.close();
    }
    else {
        std::cout << "Failed to open file" << std::endl;
    }
    return 0;
}

It's easier for us to read and for you to work with.