Question: C + + , data analysis program, This program will read from a file called automobile.csv , gathers the data and find out which car

C++, data analysis program,
This program will read from a file called automobile.csv, gathers the data and find out which car has the highest horsepower and lowest displacement.
( automobile.csv data:
names displacement horsepower
chevrolet chevelle malibu 307130
buick skylark 320350165
plymouth satellite 318150
amc rebel sst 304150
ford torino 302140
ford galaxie 500429198
)
***
I wrote a part of the program, but it doesn't work properly. my program is as follows:
#include
#include
#include
#include
using namespace std;
const int ITEMS =30;
const int MAXCHAR =130;
//function prototypes
void welcome();
void openFile(ifstream& inFile, char fileName[]);
void openFile(ofstream& outFile, 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 name[][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];
inFile.open("data.txt");
int count =0;
welcome();
cout <<"
Please enter the input file name: "<< endl;
cin.getline(fileName, MAXCHAR);
// call the function
openFile(inFile, fileName);
strcpy(fileName, "output.txt");
openFile(outFile, fileName);
// call read data function
count = readData(inFile, names, displacement, horsepower);
// call the print data function
printData(names, displacement, horsepower, count);
printData(names, displacement, horsepower, count,outFile);
return 0;
}
void welcome(){
cout << "Welcome to my Fun Data Analysis Program !"<< endl;
cout <<"
This program will read from a file called automobile.csv, gathers the data to find out which car has the highest horsepower and lowest displacement.";
}
void openFile(ifstream& inFile, char fileName[])
{
inFile.open(fileName);
if(!inFile)
{
cout << "File did not open! Program terminating!!" << endl;
exit(0);
}
}
void openFile(ofstream& outFile, 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.eof())
{
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 name[][MAXCHAR], double displacement[],double horsepower[], int count, ofstream& outFile){
outFile << fixed << showpoint << setprecision(2);
for (int i =0; i < count; i++)
{
outFile << name[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!