I'm trying to load the contents of a text file into an array of employees.
Contents of text file are as follows:
10 alice 4/23/1972 123-45-6789 support assistant
3 bob 6/7/1980 111-12-1134 logistics manager
1 carol 10/2/1963 987-123-1143 admin ceo
2 dave 10/3/1974 902-22-8914 admin cfo
17 erin 6/13/1991 126-83-1942 technology supervisor
15 frank 2/22/1987 303-12-1122 logistics assistant
The data represents ID#, name, DOB, SSN, department, and position.
Not really sure why I'm getting an error. Shouldn't the while loop simply cycle through all strings and assign them to employee New variables?
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
|
#include <iostream>
#include <fstream>
using namespace std;
struct employee
{
int id;
string name;
string dob;
string ssn;
string dept;
string pos;
};
class log
{
private:
int id;
string date;
int timeIn;
int timeOut;
};
int main()
{
string str;
employee New;
employee array[100];
int i = 0, employeenum = 0;
fstream employeeList;
employeeList.open("details.txt");
//Each time a new variable is read, the while loop will
//determine what type of variable it is.
while (employeeList >> str)
{
if (i == 0)
{
New.id = str;
i++;
continue;
}
if (i == 1)
{
New.name = str;
i++;
continue;
}
if (i == 2)
{
New.dob = str;
i++;
continue;
}
if (i == 3)
{
New.ssn = str;
i++;
continue;
}
if (i == 4)
{
New.dept = str;
i++;
continue;
}
if (i == 5)
{
New.pos = str;
array[employeenum] = New;
employeenum++;
i = 0;
continue;
}
}
//I'm testing the code with this line, but it's outputting
//an error whenever I run the program.
cout << array[0].pos << endl;
}
|
Thanks,
Jon