A Honda dealer has stored inventory information in a text file called inventory.
ID: 3770285 • Letter: A
Question
A Honda dealer has stored inventory information in a text file called inventory.txt. The file is organized in six columns as follows:
id (int)
model year (int)
model (string)
trim (string)
color (string)
engine (4 or 6 cylinders) (int)
Develop a program that does the following:
Read the data file into a struct array called Honda, sized for 10 rows of data.
Write a report to a file called accord6.txt that shows the number of six cylinder Accords in stock and a line of all information available for each car in this format:
Six Cylinder Accords in Stock
ID Year Model Trim Color Engine(4/6)
Total number of 6 cylinder Accords in stock =
Explanation / Answer
Complete Program:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Car
{
int id;
int modelYear;
string model;
string trim;
string color;
int engine;
};
int main()
{
const int MAX = 10;
Car Honda[MAX];
int count = 0;
int id;
ifstream infile;
infile.open("inventory.txt");
if(!infile)
{
cout << "Input file cannot be opened!" << endl;
system("pause"); // for Visual Studio
exit(EXIT_FAILURE);
}
infile >> id;
while(infile && count < MAX)
{
Honda[count].id = id;
infile >> Honda[count].modelYear;
infile >> Honda[count].model;
infile >> Honda[count].trim;
infile >> Honda[count].color;
infile >> Honda[count].engine;
count++;
infile >> id;
}
infile.close();
ofstream outfile("accord6.txt");
int n = 0;
outfile << "Six Cylinder Accords in Stock" << endl << endl;
outfile << left << setw(10) << "ID" << setw(10) << "Year" << setw(12) << "Model" << setw(15) << "Trim" << setw(10) << "Color" << setw(12) << "Engine(4/6)" << endl;
for(int i = 0; i < count; i++)
{
if(Honda[i].engine == 6)
{
outfile << left << setw(10) << Honda[i].id << setw(10) << Honda[i].modelYear << setw(12) << Honda[i].model << setw(15) << Honda[i].trim << setw(10) << Honda[i].color << setw(12) << Honda[i].engine << endl;
n++;
}
}
outfile << " Total number of 6 cylinder Accords in stock = " << n << endl;
outfile.close();
system("pause"); // for Visual Studio
return 0;
}
Input file: inventory.txt
Output file: accord6.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.