I have some trouble displaying the file I've read into an array of structure.
This is my struct:
struct Employee {
char staffId[5];
char fullName[30];
char phoneNum[15];
char address[40];
char email[30];
};
This is my text file (columns separated by "tab"):
1 Clark Kent 012-1449326 221, Jalan Pudu, Kuala Lumpur [email protected]
2 Bruce Wayne 013-9817470 65, Jalan Jejaka, Kuala Lumpur [email protected]
3 Peter Parker 017-6912495 26, Jalan Rajabot, Kuala Lumpur [email protected]
4 Yeoman Prince 014-1374040 22, Jalan 1/109e, Kuala Lumpur [email protected]
5 Tony Stark 016-7473151 21, Jalan Pandan, Kuala Lumpur [email protected]
6 Selina Kyle 012-4040928 Wisma Cosway, Kuala Lumpur [email protected]
7 Steve Rogers 018-9285217 Desa Pandan, Kuala Lumpur [email protected]
8 Alan Scott 019-5569400 2, Jalan U1/17, Shah Alam [email protected]
9 Britt Reid 011-7876738 43, Jalan SS2/23, Petaling Jaya [email protected]
10 Darcy Walker 011-4042788 Blok B, Setapak, Kuala Lumpur [email protected]
11 Reed Richards 019-2299339 Menara U, Bangsar, Kuala Lumpur [email protected]
12 Barbara Gordon 017-2297980 The Boulevard, Kuala Lumpur [email protected]
13 Don Diego Vega 012-4142987 10, Jalan Wangsa, Kuala Lumpur [email protected]
14 Billy Batson 013-9200151 122, Jalan Jejaka, Kuala Lumpur [email protected]
15 Barry Allen 017-7928822 Wisma Laxton, Kuala Lumpur [email protected]
16 Stanley Beamish 014-9177437 203, Sunwaymas, Batu Caves [email protected]
17 Dick Grayson 017-4023800 Pekeliling Bus, Kuala Lumpur [email protected]
18 James Howlett 012-7816910 Sri Hartamas, Kuala Lumpur [email protected]
19 Hal Jordan 013-3439897 302, Jalan Ampang, Kuala Lumpur [email protected]
20 Scott Summers 012-9057100 Menara Summit, Subang Jaya [email protected]
My code to read from the text file:
ifstream in("list.txtwith extension");
Employee *totaldata = new Employee[value + 1];
string line;
while (getline(in, line)) {
istringstream iss(line);
string token;
while (getline(iss, token, '\t')) {
// if you just want to print the information
cout << token << '\t';
// or you can store it in an Employee object
in.getline(totaldata[value].staffId, 5, '\t');
in.getline(totaldata[value].fullName, 30, '\t');
in.getline(totaldata[value].phoneNum, 15, '\t');
in.getline(totaldata[value].address, 40,'\t');
in.getline(totaldata[value].email, 30, '\t');
value++;
}
cout << endl;
for (int i = 0; i < value; i++)
{
cout << totaldata[value].staffId << "\t"
<< totaldata[value].fullName << "\t"
<< totaldata[value].phoneNum << "\t"
<< totaldata[value].address << "\t"
<< totaldata[value].email << endl;
}
I can't seem to input it in an array of structure and can't display it out?