can somebody correct my code? //#include\"stdafx.h\" #include <iostream> #includ
ID: 3826531 • Letter: C
Question
can somebody correct my code?
//#include"stdafx.h"
#include <iostream>
#include<fstream>
#include<cmath>
#include<stdlib.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()
{return dataTitle;}
string getEngineID()
{return engineID;}
string getDate()
{return date;}
string getRPM()
{return dataTitle;}
double getCompRatio()
{return compressionRatio;}
double getMaxPressure()
{return averagePressure;}
double getMinPressure()
{return averagePressure;}
double getAveragePressure()
{return averagePressure;}
private:
string dataTitle;
string engineID;
string date;
double RPM;
double pressure[SIZE];
double volume[SIZE];
int dataCount;
double compressionRatio;
double maxPressure;
double minPressure;
double averagePressure;
ifstream inputData;
ofstream report;
void openInputFile ();
void closeInputFile ();
void openOutputFile ()
{report.close();}
void closeOutputFile ();
void clearText ();
};
int main()
{
enginePerformance e;
e.retreiveData();
e.setMaxPressure();
e.setMinPressure();
e.setAveragePressure();
e.setCompressionRatio();
e.outputReport();
return 0;
}
void enginePerformance::retreiveData()
{
dataCount = 0;
openInputFile();
getline(inputData, dataTitle);
getline(inputData,engineID);
getline(inputData,date);
inputData >>RPM;
for (int i=0; i< 3; i++)
clearText();
while (inputData.eof())
{
inputData >> pressure[dataCount];
inputData >> volume [dataCount];
dataCount++;
}
}
void enginePerformance::openInputFile()
{
string filename;
cout <<"please name file."<<endl;
cin >>filename;
inputData.open("surface.txt");
if ( inputData.fail())
{
cout <<"Input file is missing"<<endl;
exit(1);
}
return;
}
void enginePerformance::clearText()
{
string garbage;
getline (inputData,garbage);
return;
}
void enginePerformance::setMaxPressure()
{
for(int j=0;j < dataCount;j++)
{
if (j == 0)
maxPressure = pressure[j];
if(pressure[j] >maxPressure)
maxPressure =pressure[j];
}
return;
}
void enginePerformance::setMinPressure()
{
for(int j=0; j<dataCount;j++)
{
if (j ==0)
minPressure =pressure[j];
if(pressure[j] <minPressure)
minPressure =pressure[j];
}
return;
}
void enginePerformance::setAveragePressure()
{
double sum = 0;
for (int j=1; j<dataCount;j++)
{
if (j ==0 )
sum =pressure[j];
else
sum =sum + pressure[j];
}
averagePressure =sum / dataCount;
return;
}
void enginePerformance::setCompressionRatio()
{
double maxVolume = 0, minVolume = 0;
for (int i=0; i < dataCount; i++)
{
if (i == 0)
maxVolume =minVolume = volume[i];
if (volume[i]>maxVolume)
maxVolume =volume[i];
if (volume[i]<minVolume)
minVolume =volume[i];
}
compressionRatio = maxVolume / minVolume;
}
void enginePerformance::openOutputFile()
{
string filename;
cout<<"Please name output file." <<endl;
cin>>filename;
inputData.open (filename);
if ( inputData.fail())
{
cout<<"Please check again you data input"<<endl;
exit(1);
}
return;
}
void enginePerformance::outputReport()
{
openOutputFile ();
report << getDataTitle() <<"report"<<endl;
report <<"Engine ID:" << getEngineID() <<endl;
report <<"Analysis Date:" << getDate() <<endl;
report <<"Engine Performance:" <<getRPM()<<" (RPM)" <<endl;
report <<"Compression Ratio :" <<compressionRatio<<endl;
report <<"Pressure"<<endl;
report <<"Max"<<maxPressure<<endl;
report <<"Min"<<minPressure<<endl;
closeOutputFile();
return;
}
Explanation / Answer
// Changes done are highlighted
// code is compiling wthout any errors now
// you can use surface.txt to use the below code and generate desired output
// C++ code
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
const int SIZE =100;
class enginePerformance
{
public:
void retreiveData();
void setMaxPressure();
void setMinPressure();
void setAveragePressure();
void setCompressionRatio();
void outputReport ();
string getDataTitle()
{return dataTitle;}
string getEngineID()
{return engineID;}
string getDate()
{return date;}
string getRPM()
{return dataTitle;}
double getCompRatio()
{return compressionRatio;}
double getMaxPressure()
{return averagePressure;}
double getMinPressure()
{return averagePressure;}
double getAveragePressure()
{return averagePressure;}
private:
string dataTitle;
string engineID;
string date;
double RPM;
double pressure[SIZE];
double volume[SIZE];
int dataCount;
double compressionRatio;
double maxPressure;
double minPressure;
double averagePressure;
ifstream inputData;
ofstream report;
void openInputFile ();
void closeInputFile ();
void openOutputFile ();
void closeOutputFile ();
void clearText ();
};
int main()
{
enginePerformance e;
e.retreiveData();
e.setMaxPressure();
e.setMinPressure();
e.setAveragePressure();
e.setCompressionRatio();
e.outputReport();
return 0;
}
void enginePerformance::retreiveData()
{
dataCount = 0;
openInputFile();
getline(inputData, dataTitle);
getline(inputData,engineID);
getline(inputData,date);
inputData >>RPM;
for (int i=0; i< 3; i++)
clearText();
while (inputData.eof())
{
inputData >> pressure[dataCount];
inputData >> volume [dataCount];
dataCount++;
}
}
void enginePerformance::openInputFile()
{
string filename;
cout <<"please name file."<<endl;
cin >>filename;
inputData.open("surface.txt");
if ( inputData.fail())
{
cout <<"Input file is missing"<<endl;
exit(1);
}
return;
}
void enginePerformance::clearText()
{
string garbage;
getline (inputData,garbage);
return;
}
void enginePerformance::setMaxPressure()
{
for(int j=0;j < dataCount;j++)
{
if (j == 0)
maxPressure = pressure[j];
if(pressure[j] >maxPressure)
maxPressure =pressure[j];
}
return;
}
void enginePerformance::setMinPressure()
{
for(int j=0; j<dataCount;j++)
{
if (j ==0)
minPressure =pressure[j];
if(pressure[j] <minPressure)
minPressure =pressure[j];
}
return;
}
void enginePerformance::setAveragePressure()
{
double sum = 0;
for (int j=1; j<dataCount;j++)
{
if (j ==0 )
sum =pressure[j];
else
sum =sum + pressure[j];
}
averagePressure =sum / dataCount;
return;
}
void enginePerformance::setCompressionRatio()
{
double maxVolume = 0, minVolume = 0;
for (int i=0; i < dataCount; i++)
{
if (i == 0)
maxVolume =minVolume = volume[i];
if (volume[i]>maxVolume)
maxVolume =volume[i];
if (volume[i]<minVolume)
minVolume =volume[i];
}
compressionRatio = maxVolume / minVolume;
}
void enginePerformance::openOutputFile()
{
string filename;
cout<<"Please name output file." <<endl;
cin>>filename;
// output file is report, should be namedd as report
report.open (filename.c_str());
if ( report.fail())
{
cout<<"Please check again you data input"<<endl;
exit(1);
}
return;
}
void enginePerformance::outputReport()
{
openOutputFile ();
report << getDataTitle() <<"report"<<endl;
report <<"Engine ID:" << getEngineID() <<endl;
report <<"Analysis Date:" << getDate() <<endl;
report <<"Engine Performance:" <<getRPM()<<" (RPM)" <<endl;
report <<"Compression Ratio :" <<compressionRatio<<endl;
report <<"Pressure"<<endl;
report <<"Max"<<maxPressure<<endl;
report <<"Min"<<minPressure<<endl;
closeOutputFile();
return;
}
// close output file
void enginePerformance::closeOutputFile()
{
report.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.