creating a class

Hello, I'm wanting to move portions of my code into separate files.

This is what I'm trying to move the code into. I've read chapters of what goes in the header file and and what is to be put in the binarylogreader.cpp but I'm lost on the first step of making the class.

BinaryLogReader_xx.h - declares the BinaryLogReader class
BinaryLogReader_xx.cpp - defines each BinaryLogReader class member function declared in BinaryLogReader_xx.h
DisplayLogFile_xx.cpp - prints the report
LogReader_xx.cpp - contains main() to test your code

Here is my code
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
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


// a global data structure to hold one data record
struct WaterLog
{
	int day, month, year;    // date
	char sector;             // 'N', 'S', 'E', 'W'
	char ID[8];              // customer ID
	char name[15];           // name of customer
	double firstM, secondM, thirdM; // number of gallons used in 1st, 2nd, 3rd month of quarter
};



/*
Function:   bool displayLogFile_binary(fstream &file)
Parameters: 
   file:  a reference to a binary log file stream
Return:
   true if the file was accessed successfully, else false
Description:
This function reads a binary water meter log file and displays its contents.
The AVERAGE column is the average water usage for the 3 month period.

SAMPLE OUTPUT:
NAME             ID         DATE         SECTOR    MONTH_1      MONTH_2   MONTH_3      TOTAL        AVERAGE
Baker            000-N01    05/28/2017   North     430.73       693.24    626.71       1750.68      583.56
Kimberly         387-W99    05/27/2017   East      822.42        98.55     60.68        981.65      327.22
*/
bool displayLogFile_binary(fstream &file)
{
	WaterLog person;  // one data record
	string sectorLog; // ??

	if (!file)
		return false;
	else
	{
		// read the first record
		file.read(reinterpret_cast<char *>(&person), sizeof(person));

		// display report header
		cout << "NAME		ID		DATE		SECTOR		MONTH_1		MONTH_2		MONTH_3		TOTAL		AVERAGE" << endl;

		// until reach end-of-file, read and display each record
		while (!file.eof())
		{
			// total amount of water used over 3 month period
			double Total = person.firstM + person.secondM + person.thirdM;

			// average amount of water used over 3 month period
			double averageWater = Total / 3;

			// change sector character into a string
			if (person.sector == 'N')
				sectorLog = "North";
			else if (person.sector == 'E')
				sectorLog = "East";
			else if (person.sector == 'S')
				sectorLog = "South";
			else if (person.sector == 'W')
				sectorLog = "West";
			else
				sectorLog = "UNKNOWN";

			// display the record
			cout << left << setw(15) << person.name << person.ID << right << setw(10) << person.month << "/" << person.day << "/" << person.year;
			cout << setw(15) << sectorLog << setw(15) << person.firstM << "		" << person.secondM << "		" << person.thirdM << "		";
			cout << Total << "		" << averageWater << endl;

			// read the next record
			file.read(reinterpret_cast<char *>(&person), sizeof(person));
		}
	}
	return true;
}
Last edited on
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
int main()
{
	// assigns name "file" as an fstream
	fstream file;


	//Opens data file "Water Log" for input in binary mode
	file.open("Water_Log.dat", ios::in | ios::binary);


	if (file)
	{

		//if file opens calls function displayLogFile_binary
		displayLogFile_binary(file);

		//closes file
		file.close();
	}
	else
	{
		//if file does not open tells user "Error opening file"
		cout << "Error opening file." << endl;
	}


	system("pause");
	return 0;
}
Topic archived. No new replies allowed.