setting string fields from file in multi-file program

Working on a class project were I was given a header, txt file, and a cpp file. I have to create a driver cpp file that holds main and uses the loadData(created not supplied) and printData(created not supplied) function calls to get information from the file and output to a txt file. I can only create the above mentioned and have to use the functions in the provided files. The program compiles but I'm not sure how to store the name and territory strings so that they can be displayed and outputted. I'm assuming I have to use the getDataFromFile function but I'm not sure how. If I can figure this out I think I get the rest of the program working.

Provided Header File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #ifndef SALES_H 
#define SALES_H 
#include<string>

using namespace std;

class SalesPerson {
private: string name; 
		 string territory; 
		 double sales[12];
		 double totalAnnualSales();
public:
	SalesPerson(); 
	~SalesPerson(); 
	void getDataFromFile(fstream &inFile); 
	void setSales(int, double); 
	void printAnnualSales(fstream &outFile); 
	void printSalesPersonInfo(fstream &outFile);
}; //end class 
#endif 



Provided cpp file
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
83
84
85
86
87
#include "sales.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//public member function definitions

SalesPerson::SalesPerson()
{
	for (int i = 0; i < 12; i++)
		sales[i] = 0.0;
}

//********************************************************

SalesPerson::~SalesPerson()
{
	for (int i = 0; i < 12; i++)
		sales[i] = 0.0;
}

//********************************************************

void SalesPerson::getDataFromFile(fstream &inFile)
{

	double sales_figure;
	int i;

	getline(inFile, name);

	getline(inFile, territory);

	for (int x = 1; x <= 12; x++)
	{
		inFile >> i;
		inFile >> sales_figure;
		cout << sales_figure << endl;
		setSales(i, sales_figure);
	}
	inFile.ignore();

}

//********************************************************

void SalesPerson::setSales(int month, double amount)
{
	if (month >= 1 && month <= 12 && amount > 0)  //test for valid month and amount value
		sales[month - 1] = amount;               //adjust for subscript 0-11
	else
		cout << "Invalid month or sales figure" << endl;
}

//********************************************************

void SalesPerson::printAnnualSales(fstream &outFile)
{
	outFile << setprecision(2) << fixed
		<< "\nTotal annual sales: $"
		<< totalAnnualSales() << endl;       // call utility member function
}

//********************************************************

void SalesPerson::printSalesPersonInfo(fstream &outFile)
{
	outFile << "\nName:" << name << endl
		<< "Territory:" << territory;
}

//********************************************************

//private utility member function

double SalesPerson::totalAnnualSales()
{
	double total = 0.0;

	for (int i = 0; i < 12; i++)
		total += sales[i];

	return total;
}


Driver File I have created:
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
#include "sales.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void loadData(SalesPerson reps[], const int SIZE, int &num_reps);
void printData(SalesPerson reps[], int num_reps);
//********************************************************
int main()
{
	const int SIZE = 25;
	SalesPerson reps[SIZE];
	int num_reps = 0;
	loadData(reps, SIZE, num_reps);
	printData(reps, num_reps);

	return 0;
}
//********************************************************
void loadData(SalesPerson reps[], const int SIZE, int &num_reps)
{
	string	name,
			territory;
	int		month,
			x = 0;
	double	amount;
	
	fstream inFile;
	inFile.open("sales_data.txt", ios::in);
	inFile >> num_reps;

	while (x < num_reps)
	{
		getline(inFile, name);
		reps[num_reps].getDataFromFile(inFile);
		getline(inFile, territory);
		reps[num_reps].getDataFromFile(inFile);
		inFile >> month >> amount;
		reps[num_reps].setSales(month, amount);
		inFile.ignore();
		++x;
		
	}
	inFile.close();
}
//********************************************************
void printData(SalesPerson reps[], int num_reps)
{
	fstream outFile;
	outFile.open("a6.txt", ios::out);

	outFile << "2016 SALES REPORT" << endl;
	outFile << "************************************"
			<< "***************" << endl;

	for (int i = 0; i < num_reps; i++)
	{
		reps[i].printSalesPersonInfo(outFile);
		reps[i].printAnnualSales(outFile);
	}
	outFile.close();
}
Last edited on
an example of the given file is:

16
Henry Johnson
Kentucky
1 789.90
2 675.77
3 123.22
4 56.89
5 612.22
6 123.11
7 900.45
8 789.90
9 340.23
10 777.50
11 1290.80
12 432.88
Calvin Adams
South Dakota
1 2189.09
2 675.77
3 123.22
4 3214.56
5 612.22
6 123.11
7 8463.99
8 789.90
9 2187.33
10 6478.50
11 1290.80
12 432.88
Louise Williams
Arizona
1 8475.99
2 2756.22
3 567.87
4 3214.56
5 612.22
6 2122.10
7 9670.90
8 857.44
9 2187.33
10 3869.00
11 1290.80
12 432.88

The first int (16) is the number of iterations the pattern repeats.
I can only create the above mentioned and have to use the functions in the provided files.

Then your functions should be calling functions from the supplied file. In other words you should be calling a function in your function to read the data from the file instead of reading the file yourself.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//********************************************************
void loadData(SalesPerson reps[], const int SIZE, int &num_reps)
{
	string	name,
			territory;
	int		month,
			x = 0;
	double	amount;
	
	fstream inFile;
	inFile.open("sales_data.txt", ios::in);
	inFile >> num_reps;

	while (x < num_reps)
	{
             // Call a function hear to read the information for record x, don't read the file with getline(). 
Ok thanks, did it but it is still not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
void loadData(SalesPerson reps[], const int SIZE, int &num_reps)
{	
	fstream inFile;
	inFile.open("sales_data.txt", ios::in);
	inFile >> num_reps;

	for (int i = 0; i < num_reps; i++)
	{
		reps[i].getDataFromFile(inFile);
	}
	
	inFile.close();
}
Why isn't it working? You need to tell us the actual problem. If you're getting compile errors you need to post them exactly as they appear in your development environment.

By the way you should always check to insure the file opened properly.

Hello nyork3415 and jlb,

Am I wrong here or is void loadData(SalesPerson reps[], const int SIZE, int &num_reps) a redefinition of void SalesPerson::getDataFromFile(fstream &inFile)?

Andy
Last edited on
Am I wrong here

While the two functions may be doing basically the same thing you can not have a redefinition when the function names are different, the parameters are different and the scope is different. A redefinition can only happen if everything is the same.
@jlb,

I stand corrected bad choice of a word. I was thinking along the lines of duplication i.e., why reinvent the wheel when there is a member function to use. Some times the brain works differently than the fingers.

Thanks for the correction jlb,

Andy
Topic archived. No new replies allowed.