Question: The revised program still can't read the data from the text file and can't get the highest horsepower and lowest displacement. When I run the

The revised program still can't read the data from the text file and can't get the highest horsepower and lowest displacement.
When I run the program,
the output below:
Welcome to the Data Analysis Program!
This program will read from a file and find out which car has the highest horsepower and lowest displacement.
Please enter the input file name: data.txt
Car with the highest horsepower is names with 0hp.
Car with lowest displacement is names with 0cc.
But the result should be read from a text file and get the highest car horsepower and lowest displacement , how can I do that, thanks
*****
Welcome to the data analysis program:
This program will read from a file called automobile.csv, gather the data and find out which car has the highest horsepower and lowest displacement.
Please enter the input file name: data.txt
names displacement horsepower
chevrolet chevelle malibu 307130
buick skylark 350165
plymouth satellite 318150
amc rebel sst 304150
ford torino 302140
ford galaxie 429198
Car with highest horsepower is ford galaxie with 198hp
Car with lowest displacement is ford torino with 302cc
Thanks for using the data analysis program.
***
#include
#include
#include
#include
using namespace std;
// Constants for number of items and maximum characters
const int ITEMS =30;
const int MAXCHAR =130;
// Function prototypes
void welcome();
void openFile(ifstream& inFile, const char fileName[]);
void openFile(ofstream& outFile, const char fileName[]);
int readData(ifstream& inFile, char names[][MAXCHAR], double displacement[], double
horsepower[]);
void printData(char names[][MAXCHAR], double displacement[], double horsepower[], int count);
void printData(char names[][MAXCHAR], double displacement[], double horsepower[], int count,
ofstream& outFile);
int main(){
ifstream inFile;
ofstream outFile;
char fileName[MAXCHAR];
double displacement[ITEMS]={0};
double horsepower[ITEMS]={0};
char names[ITEMS][MAXCHAR];
welcome();
cout << "Please enter the input file name: ";
cin.getline(fileName, MAXCHAR);
openFile(inFile, fileName);
strcpy(fileName, "output.txt");
openFile(outFile, fileName);
int count = readData(inFile, names, displacement, horsepower);
int maxHpIndex =0, minDispIndex =0;
for (int i =0; i < count; i++){
if (horsepower[i]> horsepower[maxHpIndex]){
maxHpIndex = i;
}
if (displacement[i]< displacement[minDispIndex]){
minDispIndex = i;
}
}
cout << "Car with highest horsepower is "<< names[maxHpIndex]<<" with "<<
horsepower[maxHpIndex]<<" hp."<< endl;
cout << "Car with lowest displacement is "<< names[minDispIndex]<<" with "<<
displacement[minDispIndex]<<" cc."<< endl;
printData(names, displacement, horsepower, count);
printData(names, displacement, horsepower, count, outFile);
return 0;
}
void welcome(){
cout << "Welcome to the Data Analysis Program!
";
cout << "This program will read from a file and find out which car has the highest horsepower and lowest displacement.
";
}
void openFile(ifstream& inFile, const char fileName[]){
inFile.open(fileName);
if (!inFile){
cout << "File did not open! Program terminating!" << endl;
exit(
0
);
}
}
void openFile(ofstream& outFile, const char fileName[]){
outFile.open(fileName);
if (!outFile){
cout << "File did not open! Program terminating!" << endl;
exit(
0
);
}
}
int readData(ifstream& inFile, char names[][MAXCHAR], double displacement[], double
horsepower[]){
int count =0;
while (inFile >> names[count]>> displacement[count]>> horsepower[count]){
count++;
}
inFile.close();
return count;
}
void printData(char names[][MAXCHAR], double displacement[], double horsepower[], int count){
cout << fixed << showpoint << setprecision(
2
);
for (int i =0; i < count; i++){
cout << names[i]<<""<< displacement[i]<<""<< horsepower[i]<< endl;
}
cout << endl;
}
void printData(char names[][MAXCHAR], double displacement[], double horsepower[], int count,
ofstream& outFile){
outFile << fixed << showpoint << setprecision(2);
for (int i =0; i < count; i++){
outFile << names[i]<<""<< displacement[i]<<""<< horsepower[i]<< endl;
}
outFile.close();
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!