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

Description For this assignment, you will create a car database for a dealership

ID: 3878228 • Letter: D

Question

Description

For this assignment, you will create a car database for a dealership. You will have a struct that will hold

each car’s information. The struct will be

struct automobile

{

string make ;

string model ;

unsigned int year ;

string color ;

usigned int msrp

};

Implementation details

In your program, you will have an array of type automobile that contains the cars in the dealership. The

array size will be 20 where you will use a named constant instead of a literal. This seems like very few cars

for a dealership, just assume that this is a dealership in Tokyo. The steps that you will perform will be

1. Read in the list of cars into the automobile array from a file, each line will contain one car entry, every

line will contain the make, model, year, color, and msrp in that order separated by a space and each

line will be terminated by an end of line character (you will use a command line argument to get the

file name and you will use a filestream to read through the file, the input file as well as some of the

source code can be find on the website)

2. Prompt the user to enter and attribute or q, then read in the input

(a) If the user entered an invalid attribute, output the error and ignore any other input on the line

(use cin.ignore(100, ’ ’) to ignore any input) and the repeat step 2

(b) If the user entered q, terminate the program

3. If the user entered a valid attribute (user entered ”Make”, ”Model”, ”Year”, or ”Color”), then read in

the search item and output all the cars that match the search item

4. If user entered ”list” then read the next item on the line

(a) If the user entered ”Low”, then output all the cars in sorted order according to their msrp from

high to low

1

(b) If the user entered ”High”, then output all the cars in sorted order according to their msrp from

low to high

(c) If the user didn’t enter ”Low” or ”High” then don’t do anything and go back to step 2

5. Go back to step 2

Note: the user can enter an input using upper or lower case letters, make sure your program

can handle that

Example Run:

Enter search type or ’q ’ to quit : make Honda

Make : Honda

Model : Civic

Year : 2005

Color : Silver

MSRP : 18840

Make : Honda

Model : Accord

Year : 2009

Color : White

MSRP : 23570

Make : Honda

Model : Fit

Year : 2015

Color : Blue

MSRP : 16190

Enter search type or ’q ’ to quit : moDEL CHALLENGER

Make : Dodge

Model : Challenger

Year : 2018

Color : Red

MSRP : 26995

Enter search type or ’q ’ to quit : yEaR 1986

Enter search type or ’q ’ to quit : msrp 20000

Invalid Input ! Try again ! >:(

Enter search type or ’q ’ to quit : origin germany

Invalid Input ! Try again ! >:(

Enter search type or ’q ’ to quit : list HigH

2

Make : Kia

Model : Rio

Year : 2017

Color : Orange

MSRP : 13900

Make : Hyundai

Model : Accent

Year : 2008

Color : Yellow

MSRP : 14995

Make : Toyota

Model : Yaris

Year : 2011

Color : Red

MSRP : 15635

Make : Honda

Model : Fit

Year : 2015

Color : Blue

MSRP : 16190

Make : Hyundai

Model : Elantra

Year : 2015

Color : Silver

MSRP : 16950

Make : Ford

Model : Focus

Year : 2007

Color : Blue

MSRP : 17860

Make : Toyota

Model : Carolla

Year : 2007

Color : Red

MSRP : 18550

Make : Volkswagen

Model : Jetta

Year : 2005

Color : Silver

MSRP : 18645

Make : Honda

Model : Civic

Year : 2005

Color : Silver

MSRP : 18840

3

Make : Volkswagen

Model : Golf

Year : 2009

Color : Green

MSRP : 19895

Make : Volkswagen

Model : Beetle

Year : 2015

Color : Red

MSRP : 20220

Make : Hyundai

Model : Senata

Year : 2009

Color : Red

MSRP : 22050

Make : Toyota

Model : Camry

Year : 2005

Color : Yellow

MSRP : 23495

Make : Honda

Model : Accord

Year : 2009

Color : White

MSRP : 23570

Make : Ford

Model : Mustang

Year : 2016

Color : White

MSRP : 25585

Make : Chevrolet

Model : Camaro

Year : 2015

Color : Black

MSRP : 25905

Make : Dodge

Model : Challenger

Year : 2018

Color : Red

MSRP : 26995

Make : Ford

Model : Taurus

Year : 2004

Color : White

MSRP : 27595

4

Make : Dodge

Model : Charger

Year : 2017

Color : White

MSRP : 28495

Make : Chevrolet

Model : Corvette

Year : 2011

Color : Red

MSRP : 55495

Enter search type or ’q ’ to quit : list car

Enter search type or ’q ’ to quit : q

Input file:

Honda Civic 2005 Silver 18840

Dodge Challenger 2018 Red 26995

Volkswagen Beetle 2015 Red 20220

Toyota Camry 2005 Yellow 23495

Chevrolet Camaro 2015 Black 25905

Hyundai Accent 2008 Yellow 14995

Honda Accord 2009 White 23570

Ford Focus 2007 Blue 17860

Dodge Charger 2017 White 28495

Volkswagen Jetta 2005 Silver 18645

Hyundai Senata 2009 Red 22050

Kia Rio 2017 Orange 13900

Toyota Carolla 2007 Red 18550

Chevrolet Corvette 2011 Red 55495

Ford Mustang 2016 White 25585

Honda Fit 2015 Blue 16190

Ford Taurus 2004 White 27595

Toyota Yaris 2011 Red 15635

Volkswagen Golf 2009 Green 19895

Hyundai Elantra 2015 Silver 16950

Explanation / Answer

#include<iostream>

#include <fstream>

#include <sstream>

#include <string>

#include<stdlib.h>

using namespace std;

#define NUM_OF_CARS 20 //Named constant to represent number of cars in the input file. Change this according to the number of cars in the file.

struct automobile {

string make ;

string model ;

unsigned int year ;

string color ;

unsigned int msrp;

};

//Function to print single car details

void printCar(automobile car) {

cout<<"Make: "<<car.make<<" ";

cout<<"Model: "<<car.model<<" ";

cout<<"Year: "<<car.year<<" ";

cout<<"Color: "<<car.color<<" ";

cout<<"Msrp: "<<car.msrp<<" ";

}

//Utility function to print all the car details in descenting order.

void printDesc(struct automobile cars[20])

{

int i, j;

struct automobile temp;

  

for (i = 0; i < NUM_OF_CARS - 1; i++)

{

for (j = 0; j < (NUM_OF_CARS - 1-i); j++)

{

if (cars[j].msrp < cars[j + 1].msrp)

{

temp = cars[j];

cars[j] = cars[j + 1];

cars[j + 1] = temp;

}

}

}

  

for(int i = 0;i<NUM_OF_CARS;i++) {

printCar(cars[i]);

cout<<" ";

}

}

//Utility function to print all the car details in descenting order.

void printAsce(struct automobile cars[20])

{

int i, j;

struct automobile temp;

  

for (i = 0; i < NUM_OF_CARS - 1; i++)

{

for (j = 0; j < (NUM_OF_CARS - 1-i); j++)

{

if (cars[j].msrp > cars[j + 1].msrp)

{

temp = cars[j];

cars[j] = cars[j + 1];

cars[j + 1] = temp;

}

}

}

  

for(int i = 0;i<NUM_OF_CARS;i++) {

printCar(cars[i]);

cout<<" ";

}

}

int main() {

automobile cars[NUM_OF_CARS];

fstream inFile;

inFile.open ("input.txt", fstream::in);

string line;

int i = 0;

while (std::getline(inFile, line)) {

istringstream iss(line);

string attribute;

automobile car;

int j = 1;

while(getline(iss, attribute, ' ' ) ) {

if(j==1) {

car.make = attribute.c_str();

j++;

} else if(j==2) {

car.model = attribute.c_str();

j++;

} else if(j==3) {

car.year = atoi(attribute.c_str());

j++;

} else if(j==4) {

car.color = attribute.c_str();

j++;

} else if(j==5) {

car.msrp = atoi(attribute.c_str());

j=1;

}

}

cars[i++] = car;

}

inFile.close();

while(1) {

cout<<"Enter search type or q to quit :";

string query;

getline(cin,query);

if(strcmp(query.c_str(),"q")==0)

return 0;

int pos = query.find_first_of(' ', 0);

string attr = query.substr(0,pos);

string value = query.substr(pos+1);

if(strcmpi(attr.c_str(),"Make")==0) {

for(int i = 0; i<NUM_OF_CARS;i++) {

if(strcmpi(cars[i].make.c_str(),value.c_str())==0) {

printCar(cars[i]);

}

}  

} else if(strcmpi(attr.c_str(),"Model")==0) {

for(int i = 0; i<NUM_OF_CARS;i++) {

if(strcmpi(cars[i].model.c_str(),value.c_str())==0) {

printCar(cars[i]);

}

}

} else if(strcmpi(attr.c_str(),"Year")==0) {

for(int i = 0; i<NUM_OF_CARS;i++) {

unsigned int year = atoi(value.c_str());

if(cars[i].year==year) {

printCar(cars[i]);

}

}  

} else if(strcmpi(attr.c_str(),"Color")==0) {

for(int i = 0; i<NUM_OF_CARS;i++) {

if(strcmpi(cars[i].color.c_str(),value.c_str())==0) {

printCar(cars[i]);

}

}  

} else if(strcmpi(attr.c_str(),"msrp")==0) {

int msrp = atoi(value.c_str());

if(cars[i].msrp==msrp) {

printCar(cars[i]);

}

} else if(strcmpi(attr.c_str(),"list")==0) {

if(strcmpi(value.c_str(),"Low")==0) {

printDesc(cars);

}

if(strcmpi(value.c_str(),"High")==0) {

printAsce(cars);

}

} else {

cout<<"Invalid Input ! Try again ! >:( ";

}

pos = pos+1;

}

return 0;

}