Write a program in C++ that stores cars in an array and that can add, remove, pr
ID: 3551629 • Letter: W
Question
Write a program in C++ that stores cars in an array and that can add, remove, print and list cars. The commands for adding, removing, printing and listing will come from a file (specified on either the command-line or via STDIN). Commands are as follows (fields are separated by tabs):
Here are some example commands files:
lab2-HYbridCommands.tab
The first file should produce STDOUT output that matches exactly with what's in lab2-hybridCommands-stdout.txt
There will be at most 1024 unique cars.
THIS THE https
Explanation / Answer
/* Header files section */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/* Vehicle structure */
struct Vehicle
{
int price;
int year;
int mileage;
string make;
string model;
string color;
int distance;
int count;
}; /* end of structure */
/* function protypes */
int getIndex(Vehicle *, Vehicle *);
void addCar(Vehicle *, Vehicle *);
void removeCar(Vehicle *, Vehicle *, ofstream &);
void printCar(Vehicle *,Vehicle *, ofstream &);
void printAllCars(Vehicle *, ofstream &);
/* global variable */
int nextIndex;
/* start main function */
int main()
{
/* open the input file */
ifstream inFile("lab2-HYbridCommands.tab");
if(!inFile)
{
/* display the error message and eoneit the program if the input file is not found */
cout << "The input file lab2-HYbridCommands.tab is not opened." << endl;
system("pause");
exit(1);
} // end if
/* open the output file */
ofstream outFile("lab2-hybridCommands-stdout.txt");
if(!outFile)
{
/* display the error message and eoneit the program if the output file is not found */
cout << "The output file lab2-hybridCommands-stdout.txt is not opened." << endl;
system("pause");
exit(1);
} // end if
char cmd;
Vehicle all[1024];
Vehicle one;
nextIndex = 0;
/* read the data from input file */
while(1)
{
inFile >> cmd;
if(!inFile)
return 1;
switch(cmd)
{
case 'A':
inFile >> one.price >> one.year >> one.mileage >> one.make >> one.model >> one.color >> one.distance;
if(!inFile)
break;
addCar(all, &one);
break;
case 'R':
inFile >> one.price >> one.year >> one.mileage >> one.make >> one.model >> one.color >> one.distance;
if(!inFile) break;
removeCar(all, &one, outFile);
break;
case 'P':
inFile >> one.price >> one.year >> one.mileage >> one.make >> one.model >> one.color >> one.distance;
if(!inFile) break;
printCar(all, &one, outFile);
break;
case 'L':
printAllCars(all, outFile);
break;
}
}
/* close the input and output files */
inFile.close();
outFile.close();
/* pause the system for a while */
system("pause");
return 0;
} /* end of main function */
int getIndex(Vehicle *all, Vehicle *one)
{
for(int i = 0; i < nextIndex; i++)
{
if(all[i].make == one->make)
if(all[i].model == one->model)
if(all[i].color == one->color)
if(all[i].price == one->price)
if(all[i].year == one->year)
if(all[i].mileage == one->mileage)
if(all[i].distance == one->distance)
if(all[i].count > 0)
return i;
}
return -1;
}
void addCar(Vehicle *all, Vehicle *one)
{
int idx = getIndex(all, one);
if(idx != -1)
all[idx].count++;
else
{
all[nextIndex].price = one->price;
all[nextIndex].year = one->year;
all[nextIndex].mileage = one->mileage;
all[nextIndex].make = one->make;
all[nextIndex].model = one->model;
all[nextIndex].color = one->color;
all[nextIndex].distance = one->distance;
all[nextIndex].count = 1;
nextIndex++;
}
}
void removeCar(Vehicle *all, Vehicle *one, ofstream &outFile)
{
int idx = getIndex(all, one);
if(idx != -1)
all[idx].count--;
else
outFile << " No car in the list with these details." << endl;
}
void printCar(Vehicle *all, Vehicle *one, ofstream &outFile)
{
int idx = getIndex(all, one);
if(idx == -1)
idx = nextIndex;
else
{
outFile << " Vehicle #" << idx + 1 << ":" << endl;
outFile << "--------------" << endl;
outFile << "Price: " << all[idx].price << endl;
outFile << "Year: " << all[idx].year << endl;
outFile << "Mileage: " << all[idx].mileage << endl;
outFile << "Make: " << all[idx].make.c_str() << endl;
outFile << "Model: " << all[idx].model.c_str() << endl;
outFile << "Color: " << all[idx].color.c_str() << endl;
outFile << "Distance: " << all[idx].distance << endl;
}
}
void printAllCars(Vehicle *all, ofstream &outFile)
{
outFile << " Listing all vehicles:" << endl;
outFile << "---------------------------------" << endl;
for(int i = 0; i < nextIndex; i++)
{
if(all[i].count>0)
printCar(all, &all[i], outFile);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.