1. A small sample of my input file
FirstName, LastName, Job, Department, AnnualSalary, EstimatedSalary
a1, a2, a3, a4, 1, 1
b1, b2, b3, b4, 2, 2
c1, c2, c3, c4, 3, 3
2. A part of my code. It worked.
But my teacher said
Instead:
const vector<string> explode(const string& s, const char& c)
I should create a function:
Employee& explode(const string& s, const char& c).
I don't understand?
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
|
const int N = 100;
Employee employees[N]; // Employee is a class
void ReadToArray(Employee arr[], string filename)
{
int i = 0;
ifstream file(filename);
if (file.is_open())
{
string line;
getline(file, line, '\n'); // Mean skip the first line? Right?
while (getline(file, line))
{
vector<string> v = explode(line, ',');
arr[i].setFirstName(v[0]);
arr[i].setLastName(v[1]);
arr[i].setJobTitle(v[2]);
arr[i].setDepartment(v[3]);
arr[i].setAnnualSalary(stod(v[4]));
arr[i].setEstimatedSalary(stod(v[5]));
i++;
}
}
}
void Display(Employee arr[], int size)
{
for (int i = 0; i < size; i++)
{
employees[i].printData();
}
}
int main()
{
ReadToArray(employees, "Employees.csv");
Menu();
return 0;
}
|
----------------------------------------------------------
@ne555
> Why not write a separate title so you don't have to deal with it?
I mean, in CSV forms only the content should be written, not the title.
Such as:
a, a, a, a, 1, 1
b, b, b, b, 2, 2
The content should not be written as:
title1, title2, title3, title4, title5, title6
a, a, a, a, 1, 1
b, b, b, b, 2, 2
Because we have to "cout" the title ourselves, right? We always have to skip line 1. Is this inconvenience necessary?
Thank for your time! :)