Open file and close file not working

I added some test code for my openfile function and my closefile function and they are not reading back anything. Obvious this is why my file isn't being opened or written to. Any help would be awesome, would love to finish this program off.

Thanks


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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

const int SIZE = 100;

class enginePerformance
{
public:
      void retreiveData();

      void setMaxPressure();
      void setMinPressure();
      void setAveragePressure();
      void setCompressionRatio();
      void outputReport ();

      string getDataTitle();
      string getEngineID();
      string getDate();

      double getRPM();
      double getCompRatio();
      double getMaxPressure();
      double getMinPressure();
      double getAveragePressure();

private:
      string dataTitle;
      string engineID;
      string date;
      string fileName;

      double RPM;
      double sum;
      double pressure[SIZE];
      double volume[SIZE];
      int    dataCount, p, v;

      double compressionRatio;
      double maxPressure,maxVolume;
      double minPressure,minVolume;
      double averagePressure;

      ifstream inputData;
      ofstream engineReport;

      void openOutputFile ();
      void closeOutputFile ();
      //void clearText ();
};

int main()
{
	enginePerformance myCar;

	myCar.retreiveData();
	myCar.setMaxPressure();
	myCar.setMinPressure();
	myCar.setAveragePressure();
	myCar.setCompressionRatio();
	myCar.outputReport ();

	return 0;
}

void enginePerformance::retreiveData()
{
	cout << "Input name for file you would like to open: ";

	getline (cin, fileName);
	inputData.open (fileName.c_str());

	if (inputData.fail())
	{
		cout<<"Could not find an input file by that name, please check file location/name!";
		exit(1);
	}
	getline (inputData, dataTitle);
	getline (inputData, engineID);
	getline (inputData, date);
	inputData >> RPM ;

		sum = 0, p = 0, v = 0;
		dataCount = 0;

		while (inputData>>pressure[p]>>volume[v])
		{

			sum += pressure[p];
			p++;
			v++;
			dataCount++;
		}

	inputData.close();
	return;
}

void enginePerformance::setMaxPressure()
{

	maxPressure = pressure[0];
	for (p = 0 ; p < dataCount; p++)
		{
			if (pressure[p] > maxPressure)
			maxPressure = pressure[p];
		}

	return;
}

void enginePerformance::setMinPressure()
{
	minPressure = pressure[0];
	for (p = 0; p < dataCount; p++)
	{
		if (pressure[p] < minPressure)
			minPressure = pressure[p];
		}
	return;
}

void enginePerformance::setAveragePressure()
{
	averagePressure = sum/dataCount;

	return;
}

void enginePerformance::setCompressionRatio()
{

	double maxVolume(0), minVolume(0);

	maxVolume = volume[0];
		for (v = 0 ; v < dataCount; v++)
			{
				if (volume[v] > maxVolume)
				maxVolume = volume[v];
			}
	minVolume = volume[0];
	for (v = 0 ; v < dataCount; v++)
				{
					if (volume[v] < minVolume)
					minVolume = volume[v];
				}
	compressionRatio = maxVolume/minVolume;

	cout.setf(ios::showpoint | ios::fixed);
	cout.precision(1);

	engineReport.setf(ios::showpoint | ios::fixed);
	engineReport.precision(1);

	return;
}

string enginePerformance::getDataTitle()
{
	return dataTitle;
}
string enginePerformance::getEngineID()
      {
    return engineID;
      }
string enginePerformance::getDate()
{
	return date;
}

double enginePerformance::getRPM()
{
	return RPM;
}


double enginePerformance::getCompRatio()
{
	return compressionRatio;
}

double enginePerformance::getMaxPressure()
{
	return maxPressure;
}
double enginePerformance::getMinPressure()
{
	return minPressure;
}
double enginePerformance::getAveragePressure()
{
	return averagePressure;
}

void enginePerformance::openOutputFile ()
{
	cout << "Function is working" << endl;//test code not working
	ofstream engineReport;
	engineReport.open("EngineReport.txt");

	return;
}

void enginePerformance::outputReport ()
{
	cout << "Function is working" << endl;//test code working
	engineReport << dataTitle <<endl<<endl;
	engineReport <<"Engine ID: " <<engineID <<endl<<endl;
	engineReport <<"Analysis Date: " <<date <<endl<<endl;

	engineReport <<"Engine Performance: " << RPM <<"  (RPM)"<<endl<<endl;
	engineReport <<"Compression Ratio  " <<compressionRatio << ": 1"<<endl<<endl;
	engineReport <<"Pressures"<<endl;
	engineReport <<"Maximum " <<maxPressure<<" (psi)"<<endl;
	engineReport <<"Minimum " <<minPressure<<" (psi)"<<endl;
	engineReport <<"Average " <<averagePressure<<" (psi)"<<endl;

	cout << dataTitle <<endl<<endl;
	cout <<"Engine ID: " << engineID <<endl<<endl;
	cout <<"Analysis Date: " << date <<endl<<endl;

	cout <<"Engine Performance:   "<< RPM <<"  (RPM)"<<endl<<endl;
	cout <<"Compression Ratio  " <<compressionRatio << ": 1"<<endl<<endl;
	cout <<"Pressures"<<endl;
	cout <<"Maximum " <<maxPressure<<" (psi)"<<endl;
	cout <<"Minimum " <<minPressure<<" (psi)"<<endl;
	cout <<"Average " <<averagePressure<<" (psi)"<<endl;
	return;
}

void enginePerformance::closeOutputFile ()
{
	cout << "Function is working" << endl;//test code not working
	engineReport.close();
	return;
}
.
Whereabouts to you call the function openOutputFile() ?

Also why does that function use its own local variable named engineReport which will hide the one defined as a member of the class?
Last edited on
Verbally I don't think I am understanding correctly but I will try to explain. The verbage for class and objects is still a bit vague to me but I thought openOutputfile is just the name of the void member function. So I thought that I needed to actually make a call either engineReport or whatever that actually opens a file.

Your questions make me think that your asking why didn't I just use openOutputFile, to which I would ask why would it matter if that is just a void member function of the class mycar?
Last edited on
At line 69, you call the function outputReport().

But I don't see anywhere that the function openOutputFile() is being called.

If it was a public member function, you could call it from main(), just before the call to outputReport(). But it is better the way you have it now, it is private, so you must call it from within another member function. A good place would be at the start of outputReport (about line 214), before attempting to write anything to the output file, first open it.


Though after you have solved the current issue, you might consider some reworking of the code, to remove what is almost two sets of identical code within function outputReport. But that's a separate topic.
That was the part i didn't understand I made the edit and now it opens the output file but isn't writing correctly to it. I will debug it and see what I can find but you helped me get the output file to open, thanks a ton.
Thanks a lot chervil, I figured out my problem I did another ofstream down in the function and it was overwriting my private variable ofstream. I removed it and everything works.
Yes, I already mentioned that. The local variable within the function hides the one with the same name outside the function.

See tutorial:
http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
Topic archived. No new replies allowed.