Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ that keeps track of a car database. The program should lo

ID: 3918276 • Letter: W

Question

Write a program in C++ that keeps track of a car database. The program should load the cars information from a data file once the program is started. It should allow user to view, add, remove, and search for cars. The program should save the data back to the same data file when the program exits. All the cars should be stored in an array. Also this program needs to work on unix as well as windows.

Goals for This Project: Using class to model Abstract Data Type OOP-Data Encapsulation Use C strings and functions Read from data file and write to data file using fstream Problem You Need to Solve for This Project You are asked to write an app to keep track of a car database. The app should load the cars information from a data file once the app is started. It should allow user to view, add, remove, and search for cars. The app should save the data back to the same data file when the program exits What Your Program Should Do Write an interactive text based menu interface (using a loop) that will allow the user to Enter information for a new car Display information for all the cars in the database with index for each car Remove a car by index .Search for cars by a certain origin (give the user a list of enum choices using letters or numbers) .Search for cars by a certain name (partial string should work) Search for cars by model or any other field of your choice Quit For each car, you need to keep track of Car:MPG:Cylinders;Displacement:Horsepower;Weight; Acceleration;Model:Origin The data types for the above data would be CHAR ARRAY;DOUBLE:INT;DOUBLE:DOUBLE;DOUBLE DOUBLE INT CAT where CAT is an enumerated data type used to keep track of the Origin data.

Explanation / Answer

include <iostream>

#include <string>

#include <fstream>

#include <stdio.h>

using namespace std;

struct Vehicle // struct Vehicle

{

int price;

int year;;

string model;

//If all features of two cars are same, then we incriment the frequency.

int freq;   

};

// FUNCTION PROTOTYPES

int NextFreeIndex; // where we can add a new unique car.

void Add(Vehicle *car, Vehicle *x);

void Remove(Vehicle *car, Vehicle *x);

void Print(Vehicle *car,Vehicle *x);

void ListAll(Vehicle *car);

int findInArray(Vehicle *car, Vehicle *x);

int main()

{

ifstream myInput("lab2-hybridCommands.tab");

if(!myInput)

{

cout << "Cannot open lab2-hybridCommands.tab"<<endl; // error checking for wrong file

return 1;

}

Vehicle car[1024], x;

char command;

NextFreeIndex = 0;

while(1)

{

myInput >> command;

if(!myInput) return 1;

switch(command){

case 'A':

myInput >> x.price >> x.year >> x.model;

if(!myInput) break;

Add(car,&x);

break;

case 'R':

myInput >> x.price >> x.year>> x.model;

if(!myInput) break;

Remove(car,&x);

break;

case 'P':

myInput >> x.price >> x.year >>x.model;

if(!myInput) break;

Print(car,&x);

break;

case 'L':

ListAll(car);

break;

}

}

cout << "input file does not have the correct format."<<endl;

return 1;

}

// FUNCTION DEFINTIONS

int findInArray(Vehicle *car, Vehicle *x)

{

// Searches for vehicle x in car array

// returns the index (if found) or -1(not found)

for(int i=0; i< NextFreeIndex; i++)

{

if(car[i].model == x->model)

if(car[i].price == x->price)

if(car[i].year == x->year))

if(car[i].freq>0)

return i;

}

return -1;

}

// ADD FUNCTION

void Add(Vehicle *car, Vehicle *x)

{

int j = findInArray(car,x);

//If a vehicle having exact features is present it will incriment the freq

if(j!=-1)

{

car[j].freq++;

}

//If its a new vehicle add it at NextFreeIndex

else

{

car[NextFreeIndex].price = x->price;

car[NextFreeIndex].year = x->year;

car[NextFreeIndex].model = x->model;

car[NextFreeIndex].freq = 1;

NextFreeIndex++;

}

}

// REMOVE FUNCTION

void Remove(Vehicle *car, Vehicle *x)

{

int j = findInArray(car,x);

//If a vehicle having exact features is present decriment the frequency.

if(j!=-1)

{

car[j].freq--;

}

else

{

cout << "No such car in the database" << endl;

}

}

// PRINT FUNCTION

void Print(Vehicle *car,Vehicle *x)

{

int j = findInArray(car,x);

if(j==-1) j=NextFreeIndex;

printf("Vehicle #%d: ",j+1);

printf("-------------- ");

printf("Price: %d Year: %d Miles: %d Make: %s Model: %s Color: %s Dist.: %d ",car[j].price,car[j].year,car[j].mileage,car[j].make.c_str(),car[j].model.c_str(),car[j].color.c_str(),car[j].distance);

}

// LIST FUNCTION

void ListAll(Vehicle *car)

{

cout<<"Listing all vehicles: --------------------------------- ";

for(int i=0;i<NextFreeIndex;i++)

{

if(car[i].freq>0) Print(car,&car[i]);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote