Class/Object/String Help

When running my code it does not compute my values from my txt file correctly. I am attempting to use first number method.

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
 

#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;
      char rpM[];
      double pressure[SIZE];
      double volume[SIZE];
      int    dataCount, i;

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

	inputData >> dataCount;
		for (i = 0; i < dataCount; i++)
		{
			inputData >> pressure[i] >> volume[i];
		}

	inputData.close();
	return;
}

void enginePerformance::setMaxPressure()
{
	maxPressure = pressure[0];
	for (i = 0 ; i < dataCount; i++)
		{
			if (pressure[i] > maxPressure)
			maxPressure = pressure[i];
		}
	return;
}

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

void enginePerformance::setAveragePressure()
{

	sum += pressure[i];
	averagePressure = sum/dataCount;

	return;
}

void enginePerformance::setCompressionRatio()
{
	double maxVolume(0), minVolume(0);

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

	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 ()
{
	cout.setf(ios::showpoint | ios::fixed);
	cout.precision(3);

	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:   Speed" << 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;
}


.txt file:

Engine Analysis
Megatech Mark III Model TE1 Serial Number 155
January 10, 2010
1800 Engine RPM
46
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
First, line 43. What is the size of that array?

Second, you don't check whether any of your >> operations actually succeed. If they fail, ...
Size of the array is: Engine RPM, so 10. I wanted it to auto adjust to the argument. I have not mastered the skill of test code. I have been shown by the teacher and by my boss but its not something I all the way understand when and where to use just yet.

I will try but I still think there is something wrong with a small piece of code and I just have no clue where to start
Size of the array is: Engine RPM, so 10. I wanted it to auto adjust to the argument.

However, the compiler cannot know that. All it knows is that you wrote char rpM[]; and it has to generate instructions that allocate exactly that many bytes. Is that 0 bytes?

Use std::string. You can use getline, just like you did with the previous lines.

Besides, the >> does formatted input and whitespace is a delimiter. Thus, you current code reads only the first word (Engine) and leaves the "RPM" to stream. Guess what happens in the inputData >> dataCount; then?

No. Do not guess. Read the http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Therein: "no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type."

The getline to string reads both words and the std::string auto adjusts -- plain arrays do not.


Nevertheless, the inputData >> dataCount; could fail. What to do?
Test the stream: http://www.cplusplus.com/reference/ios/ios/operator_bool/

Rather than:
1
2
inputData >> dataCount;
// read data 

do
1
2
3
4
if ( inputData >> dataCount ) {
  // now we know that dataCount is a valid number
  // read data
}

There can still be problems, so instead of
1
2
3
4
5
inputData >> dataCount;
for (i = 0; i < dataCount; i++)
{
  inputData >> pressure[i] >> volume[i];
}

do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
dataCount = 0;
if ( inputData >> dataCount ) {
  if ( SIZE < dataCount ) dataCount = SIZE; // arrays cannot take more than SIZE
  double pres;
  double vol;
  int count = 0;
  while ( count < dataCount && inputData >> pres >> vol ) {
    // we got a valid pres and vol
    pressure[count] = pres;
    volume[count] = vol;
    ++count;
  }
  dataCount = count; // real value
}


The getlines can fail too, but if they have failed, then inputData is already in failed state and reading to dataCount fails too.


PS. Do not use exit(). That is a harsh way to quit. Rather make the caller check and handle errors somehow. (For example, if dataCount is 0 after retreiveData() ...)
Topic archived. No new replies allowed.