Write a computer program in C++ that stores cars in an array and that can add, r
ID: 3550935 • Letter: W
Question
Write a computer 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
I have tested the code with the filenames as you suggested, it just works perfect!! CHEERS !!
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;
struct Vehicle
{
string price;
string year;
string mileage;
string make;
string model;
string color;
string distance;
};
void add_car(Vehicle v, Vehicle all[], int e);
int* remove_car(Vehicle v, Vehicle all[], int last, int* empty);
void print_car(Vehicle v);
void list_all(Vehicle all[], int last, int arr[]);
void add_car(Vehicle v, Vehicle all[], int e)
{
// e denotes the empty position in array where the vehicle needs to be added
all[e].price=v.price;
all[e].year=v.year;
all[e].mileage=v.mileage;
all[e].make=v.make;
all[e].model=v.model;
all[e].color=v.color;
all[e].distance=v.distance;
}
//as per the question requirements, here a pointer is used in argument
int* remove_car(Vehicle v, Vehicle all[], int last, int* empty)
{
int res;
//last denote the last-1 index in array that has a vehicle
for(int e=0;e<last;e++)
{
if( all[e].price==v.price && all[e].year==v.year && all[e].mileage==v.mileage && all[e].make==v.make && all[e].model==v.model && all[e].color==v.color && all[e].distance==v.distance)
{
all[e].price="";
all[e].year="";
all[e].mileage="";
all[e].make="";
all[e].model="";
all[e].color="";
all[e].distance="";
res=e;
empty=&res; //now eth index is empty, so this needs to be stored, new vehicle may be added here
return empty;
}
// assuming only 1 entry is removed
}
}
void print_car(Vehicle v)
{
ofstream a_file;
a_file.open("lab2-hybridCommands-stdout.txt",ios_base::app);
a_file<<v.price<<endl;
a_file<<v.year<<endl;
a_file<<v.mileage<<endl;
a_file<<v.make<<endl;
a_file<<v.model<<endl;
a_file<<v.color<<endl;
a_file<<v.distance<<endl;
a_file<<endl;
a_file.close();
}
void list_all(Vehicle all[], int last, int arr[])
{
ofstream a_file;
a_file.open("lab2-hybridCommands-stdout.txt",ios_base::app);
a_file<<"listing all vehicles : "<<endl;
a_file.close();
for(int e=0;e<last;e++)
{
a_file.open("lab2-hybridCommands-stdout.txt",ios_base::app);
a_file<<"Vehicle #"<<e+1<<endl;
a_file.close();
if(arr[e] ==1)
print_car(all[e]); //print only those entries that has a vehicle, not which were deleted
}
}
int main()
{
Vehicle v_data[10000];
int empty_locations[10000];
for(int i=0;i<10000;i++)
empty_locations[i]=-1;
string outputFile="lab2-hybridCommands-stdout.txt";
ifstream input("lab2-HYbridCommands.tab"); //your given input file
string line; string data[8];
char *pch; int last=0;
while( getline( input, line ) ) {
// to tokenize the string, we convert the string into char array, then use strtok function
char temp[line.size()];
strcpy(temp, line.c_str());
pch=strtok(temp," ");
int k=0;
while (pch != NULL)
{
data[k]= string(pch);
pch = strtok (NULL, " ");
k++;
}
//end of this loop, data array has all string
if(data[0]== "A")
{
Vehicle v1;
v1.price=data[1];
v1.year=data[2];
v1.mileage=data[3];
v1.make=data[4];
v1.model=data[5];
v1.color=data[6];
v1.distance=data[7];
//search for the index at which vahicle is to be added in array empty_locations
int flag=0; int var;
for(int i=0;i<10000;i++)
{
if(empty_locations[i] == -1)
{flag=1; var=i; break;}
}
if(flag==1) {add_car(v1,v_data,var); empty_locations[var]=1;} //location var has now a vehicle hence not empty
else {add_car(v1,v_data,0); empty_locations[0]=1;} //all locations are empty denoted by -1, add in 1st index
last++; //last = index at which new vehicle can be added
}
else if(data[0]== "R")
{
Vehicle v1;
v1.price=data[1];
v1.year=data[2];
v1.mileage=data[3];
v1.make=data[4];
v1.model=data[5];
v1.color=data[6];
v1.distance=data[7];
int* empt; int* temp;
temp=remove_car(v1, v_data,last,empt);
int res=*temp;
empty_locations[res]=-1; //*empt was the location that became empty, thus has -1
}
else if(data[0]== "P")
{
Vehicle v1;
v1.price=data[1];
v1.year=data[2];
v1.mileage=data[3];
v1.make=data[4];
v1.model=data[5];
v1.color=data[6];
v1.distance=data[7];
print_car(v1);
}
else if (data[0]== "L") {list_all(v_data,last,empty_locations);}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.