I have two files which basically look like this:
file1:
232 JOHN SMITH 3.4 34 H
345 ADAM SILVER 3.6 3 A
|
file2:
232 4.0 S 2 Y
345 -3.0 M 4 N
|
I'm trying to read the info by first defining a struct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct Employee
{
char ID[10];
char last[30];
char first[30];
double wage;
double hrs;
char dept;
double change;
bool single;
int depend;
bool ins;
};
| |
And then read the info to a vector of struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
vector<Employee> employ;
checkFileName();
ifstream in1(file1);
ifstream in2(file2);
char yn;
int n = 1;
while ( ! ( in1.eof() || in2.eof()) )
{
Employee temp;
in1 >> temp.ID >> temp.last >> temp.first >> temp.wage >> temp.hrs >> temp.dept;
in2 >> temp.ID >> temp.change >> yn >> temp.depend;
temp.single = toupper(yn) == 'S';
in2 >> yn;
temp.ins = toupper(yn) == 'Y';
employ.push_back(temp);
}
in1.close();
in2.close();
| |
And the print the vector content:
1 2 3 4 5 6 7 8 9 10 11 12
|
for ( int n = 0; n < employ.size(); ++n)
cout << setw(5) << employ[n].ID
<< setw(10) << employ[n].last
<< setw(10) << employ[n].first
<< setw(4) << employ[n].wage
<< setw(4) << employ[n].hrs
<< setw(2) << employ[n].dept
<< setw(5) << employ[n].change
<< setw(2) << ( employ[n].single ? "S" : "M")
<< setw(3) << employ[n].depend
<< setw(2) << ( employ[n].ins ? "Y" : "N") << endl << endl;
| |
I expected the output to be:
232 JOHN SMITH 3.4 34 H 4.0 S 2 Y
345 ADAM SILVER 3.6 3 A -3.0 M 4 N
|
which is logically correct!
But for some reason what I got was actually:
232 JOHN SMITH 3.4 34 H 4.0 S 2 Y
345 ADAM SILVER 3.6 3 A -3.0 M 4 N
3.6 3 A -3.0 M 4 N
|
(Notice the last line, plz!)
This seems kinda odd to me!
I have no idea what's really goin' on, but my guess is that my code didn't stop reading even when it encountered the EOF marker, and worse it might've even moved back the pointer and read the last line again~!:S But why did it skip "345", "ADAM", AND "SILVER" ?!
Anyways, can anyone help me w/ this?