this questions for C++ for how to use getline() function get all the data from input file.

this questions for C++ for how to use getline() function get all the data from input file.

I have input file like that:

1039547
Jabob Smith
120000

1033547
Gwenn Mccloy
Male
2

Hildegard Rutz
39.46

Lydia Feng
58.21

1478021
Hildegard Rutz
Female
1

Noreen Encarnacion
1.23

1098576
Deirdre Stayer
Female
4

Wade Peffer
5.24

Ronda Ord
89.99

Lacresha Lawrence
22.54

Zackary Schiro
19.95

122354
Deirdre Stayer
Female
1

Marcus Curl
2.22

187213
Tyesha Overton
Male
2

Celine Mccutchan
52.29

Debra Widger
2.55

I am trying to create the get data function to get all the data from the input file. the first people in the input file is manager which just have one with manager id , name and salary.

the manager have 5 salesman which has slaesman id, name gender, and no of customer. customer information is under salesman which just have name and sales.

I create one class for manager to contain all the the information of manager. one array for salesman to contain all the information for salesman. one struct in salesman array to contain all the customer. below is my declearation of all the data and the function to get all the data from file.

however, I cannot use getline(inData,SalesmanList[i].SalesmanName; to get the salesman name. could any of you fix my get data function?

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
  class manager
{
public:
string ManagerId;
string ManagerName;
double ManagerSalary;
manager()
{
ManagerId="";
ManagerName="";
ManagerSalary=0.0;
}

};
class salesman
{
public:
struct customer
{

string CustomerName;
double CustomerSpend;
customer()
{
CustomerName="";
CustomerSpend=0.0;
}
};
string SalesmanId;
string SalesmanName;
int CustomerNo;
double totalsales;
string SalesmanGender;
vector Customer;
 salesman()
 {
 SalesmanId="";
 CustomerNo=0;
 totalsales=0.0;
 SalesmanName="A";
 SalesmanGender="";
 }
// bool comparesales();

 };

void getdata(manager &ManagerList, salesman SalesmanList[])
{
ifstream inData;
inData.open("input.txt");
getline(inData,ManagerList.ManagerId);
getline(inData,ManagerList.ManagerName);
inData>>ManagerList.ManagerSalary;
//getline(inData,ManagerList.ManagerSalary);
string tmp;
for(int i=0;i<5;i++)
{

//inData>>SalesmanList[i].SalesmanId;
getline(inData, tmp);
getline(inData,SalesmanList[i].SalesmanId);
if (getline(inData,SalesmanList[i].SalesmanName)) //test the read for success
{
cout << SalesmanList[i].SalesmanName << endl;
}
else
{
cout << "failed to read file" << endl;
}
//getline(inData,SalesmanList[i].SalesmanName);
getline(inData,SalesmanList[i].SalesmanGender);
inData>>SalesmanList[i].CustomerNo;
for(int j=0;j {
getline(inData,SalesmanList[i].Customer[j].CustomerName);
inData>>SalesmanList[i].Customer[j].CustomerSpend;
}
}

inData.close();
}
You have a file structure problem. As far as I can tell, from reading the file a line at a time you can't determine what kind of entry you have.
Your first problem is not knowing how to format your code.

Topic archived. No new replies allowed.