While Statement eof Marker

The problem is at line 87 under sum =0; I was trying to write a eof method to read my dual array but it keeps crashing my .cpp file. I do not understand whats wrong, especially since I am not being given an error my my compiler.

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
246

#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;

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

      double compressionRatio;
      double maxPressure;
      double minPressure;
      double averagePressure;

      ifstream inputData;
      ofstream report;

      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()
{
		inputData.open ("data.txt");
	if (inputData.fail())
	{
		cout<<"Input file not saved in the correct location!";
		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()
{
	cout.setf(ios::showpoint | ios::fixed);
	cout.precision(3);

	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;

	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 ()
{

	ofstream report;
	report.open("Report");

	return;
}

void enginePerformance::outputReport ()
{


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

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

	cout << dataTitle <<endl<<endl;
	cout << engineID <<endl<<endl;
	cout << 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 ()
{

	report.close();
	return;
}

Last edited on
dataCount is never assigned any value.

85
86
87
88
89
90
91
92
    sum = 0;
    int i = 0;
    while (inputData>>pressure[i]>>volume[i])
    {
        sum += pressure[i];
	i++;
    }
    dataCount = i;


Though it isn't a problem, there doesn't seem a good reason for making i a member variable at line 43, it is just a temporary variable, which is probably better declared as and when needed, since the value it contains has no meaning outside of the place where it has been used.
I figured out part of my problem, I cannot use i for both volume and pressure so I changed those to p and v respectively but my max and min values don't come out right
The problem is that on line 87 the member variable i is [probably] uninitialized.

Take a look at line 89 and 91. Can you see what's wrong with it?

Better make i local within the functions.

And by the way: dataCount is never initialized. Other variable might remain unitialized too.
Use the constructor enginePerformance::enginePerformance() to initialze the member variables.
I realized that I am trying to run a for loop after my while loop already completely the end of file. So I will need to change my set min and set max functions so that it reads differently.
For anyone who ever comes across this problem, I just didn't add v++ and p++ to my while loop.
Code solved added above!

Incase someone wants to run the file here is the input file named data.txt

Engine Analysis
Megatech Mark III Model TE1 Serial Number 155
January 10, 2010
1800
49 1.52
65 1.38
87 1.46
122 1.56
98 1.72
64 2.16
45 2.71
34 3.33
28.5 3.94
25 4.5
23 4.95
17 5.27
13 5.46
12 5.49
12.5 5.49
13 5.36
14.5 5.08
15 4.68
15.5 4.15
15.5 3.56
16 2.90
16 2.33
16 1.85
16 1.52
16 1.38
15 1.44
15 1.71
13.5 2.17
13 2.73
12 3.34
12 3.94
12 4.50
12.5 4.95
13 5.27
14 5.46
15 5.50
16 5.36
16.5 5.08
19 4.67
22 4.15
25 3.56
32 2.90
37 2.33
44 1.85
47 1.52
49 1.52
Last edited on
Topic archived. No new replies allowed.