Using ofstream with structs

Hello all, thank you for taking your time to view this issue. I am currently reading in a .csv file with stats from 2013 and another 2014 stat file. I am attempting create and output a master file containing that stats from both files.

Below is the code I have worked on. Inputting the files seems to be running smoothly but I am stuck on how to extract the information I have stored in the array of structs to create a master file. As you can see below, I tried to extract one string (firstName) from the playerStats2013 array to no avail.

Any help would be much appreciated, I am simply stuck and am looking for assistance.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	
	struct statsFrominFileStruct2013
	{
		string Type, playerID, firstName, lastName, Division, Team;
		int plateApps, atBats, Singles, Doubles, Triples,
			homeRuns, Hits, sacFlys, hitByPitch;
		double batAvg, onBase, Slugger;
	};

	
	statsFrominFileStruct2013 playerStats2013[500];
	int count = 0;

	ifstream inFile2013;
	inFile2013.open("Master_Data.csv", ios::in);


	if (inFile2013.is_open()) {
		while (!inFile2013.eof()) {
			getline(inFile2013, playerStats2013[count].playerID, ',');
			getline(inFile2013, playerStats2013[count].firstName, ',');
			getline(inFile2013, playerStats2013[count].lastName, ',');
			getline(inFile2013, playerStats2013[count].Division, ',');
			getline(inFile2013, playerStats2013[count].Team, ',');
			inFile2013 >> playerStats2013[count].plateApps;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].atBats;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].Singles;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].Doubles;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].Triples;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].homeRuns;
			inFile2013.ignore();
			playerStats2013[count].Hits = playerStats2013[count].Singles
				+ playerStats2013[count].Doubles + playerStats2013[count].Triples +
				playerStats2013[count].homeRuns;
			inFile2013 >> playerStats2013[count].sacFlys;
			inFile2013.ignore();
			inFile2013 >> playerStats2013[count].hitByPitch;
			inFile2013.ignore();
			playerStats2013[count].batAvg = playerStats2013[count].Hits / playerStats2013[count].atBats;

			playerStats2013[count].onBase = (playerStats2013[count].Hits) / (playerStats2013[count].atBats +
				playerStats2013[count].sacFlys);

			playerStats2013[count].Slugger = (playerStats2013[count].Singles +
				playerStats2013[count].Doubles + playerStats2013[count].Triples)
				/ playerStats2013[count].atBats;
		}
		count++;

	}
	inFile2013.close();

	statsFrominFileStruct2013 playerStats2014[500];
	count = 0;

	ifstream inFile2014;
	inFile2014.open("2014_League_Stats_v2.csv", ios::in);

	if (inFile2014.is_open()) {
		while (!inFile2014.eof()) {
			getline(inFile2014, playerStats2014[count].Type, ',');
			getline(inFile2014, playerStats2014[count].playerID, ',');
			getline(inFile2014, playerStats2014[count].firstName, ',');
			getline(inFile2014, playerStats2014[count].lastName, ',');
			getline(inFile2014, playerStats2014[count].Division, ',');
			getline(inFile2014, playerStats2014[count].Team, ',');
			inFile2014 >> playerStats2014[count].plateApps;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].atBats;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].Singles;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].Doubles;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].Triples;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].homeRuns;
			inFile2014.ignore();
			playerStats2014[count].Hits = playerStats2014[count].Singles + playerStats2014[count].Doubles
				+ playerStats2013[count].Triples + playerStats2013[count].homeRuns;
			inFile2014 >> playerStats2014[count].sacFlys;
			inFile2014.ignore();
			inFile2014 >> playerStats2014[count].hitByPitch;
			inFile2014.ignore();
			playerStats2014[count].batAvg = playerStats2014[count].Hits / playerStats2014[count].atBats;

			playerStats2014[count].onBase = (playerStats2014[count].Hits) / (playerStats2014[count].atBats +
				playerStats2014[count].sacFlys);

			playerStats2014[count].Slugger = (playerStats2014[count].Singles + playerStats2014[count].
				Doubles + playerStats2014[count].Triples) / playerStats2014[count].atBats;
		}
		count++;

	}
	inFile2014.close();


	ofstream outFile;
	outFile.open("trialMaster.txt", ios::out);
	if (outFile.is_open()) {
		outFile << playerStats2013[1].firstName << endl;
	}
	outFile.close();

	return 0;

}
Topic archived. No new replies allowed.