Hello, I am having difficulty on extracting inout file, using C++. The instructi
ID: 3735473 • Letter: H
Question
Hello, I am having difficulty on extracting inout file, using C++. The instruction is as follow.
"a manager always has 5 salesmen working for them. The next item to extract from the file is a salesmen’s information. A salesman has an ID, a name, a gender, and an indication of how many customers they have. After a salesman’s information comes the information of all the customers that this worker is selling to. For example, if a salesman sells to two customers, the next two entries will be customer information (here an “entry” begins and ends with a blank line). A customer’s only information is a name and a total amount of money they have spent. This same pattern is repeated until all 5 salesmen and their customers are printed."
For every input file the manager information comes first. Then each salesman followed by their customers data. The number of customers vary depending on the salesperson. What am trying to do is first read the manager information, then just only the salesman's information, then the customers. Since the customers data comes between the salesman information am having difficulty getting each information as follow.
Help please. Thank you!!!
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#define MAX 20
//Create Customer class
class Customer
{
private:
string name;
double money;
public:
void setName(string n)
{
name = n;
}
void setMoney(double m)
{
money = m;
}
string getName()
{
return name;
}
double getMoney()
{
return money;
}
};
//Create Salesman class
class Salesman
{
private:
string id;
string name;
string gender;
int numCustomers;
double sales=0;
public:
Customer c[MAX];
void addToSales(double s)
{
sales += s;
//cout << "sale= " << getSales() << endl;
}
void setID(string i)
{
id = i;
}
void setName(string n)
{
name = n;
}
void setGender(string g)
{
gender = g;
}
void setNumCutomers(int n)
{
numCustomers = n;
}
string getID()
{
return id;
}
string getName()
{
return name;
}
string getGender()
{
return gender;
}
int getNumCutomers()
{
return numCustomers;
}
double getSales()
{
return sales;
}
};
//Create Manager class
class Manager
{
private:
string id;
string name;
double salary;
public:
Salesman s[MAX];
void setID(string i)
{
id = i;
}
void setName(string n)
{
name = n;
}
void setSalary(double s)
{
salary=s;
}
string getID()
{
return id;
}
string getName()
{
return name;
}
double getSalary()
{
return salary;
}
};
int main()
{
//Create array of manager objects
Manager m[MAX];
//Also delcare necessary varaibles
ifstream readFile;
string tempStr;
int tempInt;
double tempDbl;
int mCount = 0;
int sCount = 0;
int cCount = 0;
//Open input file
readFile.open("salesinfo.txt");
//Read data from file
while (!readFile.eof())
{
//Read mananger's id
getline(readFile, tempStr);
while (tempStr == "")
{
getline(readFile, tempStr);
}
//Set manager's id
m[mCount].setID(tempStr);
//Read mananger's name
getline(readFile, tempStr);
//Set manager's name
m[mCount].setName(tempStr);
//Read manager's salary
readFile >> tempDbl;
//Set manger's salary
m[mCount].setSalary(tempDbl);
sCount = 0;
//Read five sales persons data
while (sCount < 5)
{
//Read salesperson's id
getline(readFile, tempStr);
while (tempStr == "")
{
getline(readFile, tempStr);
}
//Set salesperson's id
m[mCount].s[sCount].setID(tempStr);
//Read salesperson's name
getline(readFile, tempStr);
//Set salesperson's name
m[mCount].s[sCount].setName(tempStr);
//Read salesperson's gender
readFile >> tempStr;
//Set salesperson's gender
m[mCount].s[sCount].setGender(tempStr);
//Read number of custmers
readFile >> tempInt;
//Set number of custmers
m[mCount].s[sCount].setNumCutomers(tempInt);
cCount = 0;
//Read each customer data
while (cCount < m[mCount].s[sCount].getNumCutomers())
{
//Read customer's name
getline(readFile, tempStr);
while (tempStr == "")
getline(readFile, tempStr);
//Set customer's name
m[mCount].s[sCount].c[cCount].setName(tempStr);
//Read money spent by customer
readFile >> tempDbl;
//Set money spent by customer
m[mCount].s[sCount].c[cCount].setMoney(tempDbl);
//Add money spent by customer to salesperson's sale
m[mCount].s[sCount].addToSales(tempDbl);
cCount++;
}
sCount++;
}
mCount++;
}
//Now display the Manager, salespersons and customers data
for (int i = 0; i < mCount; i++)
{
//Print manager's details
cout << "Manager: " << m[i].getName() <<" "<< m[i].getSalary()<<endl;
cout << "_________________________________________" << endl;
//Print each salesperson's details and thier customers details
for (int j = 0; j < 5; j++)
{
cout << " SalesPerson: " << m[i].s[j].getName()<<" "<<m[i].s[j].getID()<<" "<< m[i].s[j].getSales()<<endl;
cout << "_______________________________________________________________________" << endl;
//Print customer details
for (int k = 0; k < m[i].s[j].getNumCutomers(); k++)
{
cout << " Customer: " << m[i].s[j].c[k].getName() << " " << m[i].s[j].c[k].getMoney() << endl;
}
}
}
cout << "=======================================================================" << endl;
return 0;
}
salesinfo.txt:
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.