Answer to Question #278859 in C++ for nimo

Question #278859

A car customer want to buy a car, he/she will tell the owner his/her requirements and in result the car will be shown to her along with price. For instance engine should be patrol, car cc will be 100-1299, type will be hatchback and there should be 3 colors say white, black and blue. You are required to make a program where there will b atleast 3 different make for example Toyota, honda and Suzuki. If exact match couldn’t find then your program will tell user similar to the match.

Make above program using C++ functions

1
Expert's answer
2021-12-12T12:18:55-0500
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

struct Car
{
	string brand;
	string color;
	string engineType;
	string carType;
	int carCC;
	Car(){}
	Car(string _brand, string _color, string _engineTypes, string _carType, int _carCC){}
};


void initialCars(Car *c,int num)
{
	string brands[] = { "Toyota","Honda","Suzuki" };
	string colors[] = { "white","black","blue" };
	string engineTypes[] = { "petrol","injector","diesel","gas","electric" };
	string carTypes[] = { "sedan","hatchback","cabriolet","crossover" };
	srand(static_cast<unsigned int>(time(0)));
	for (int i = 0; i < num; i++)
	{
		c[i].brand = brands[rand() % 3];
		c[i].color = colors[rand() % 3];
		c[i].engineType = engineTypes[rand() % 5];
		c[i].carType = carTypes[rand() % 4];
		c[i].carCC = rand() % 1199 + 100;
	}
}

void DisplayCar(Car c)
{
	cout << c.brand << "\t"
		<< c.color << "\t"
		<< c.engineType << "      "
		<< c.carType << "\t"
		<< c.carCC << "\n";
}

void requireCar(Car *c,int num)
{
	Car req;
	cout << "Please, enter a brand of car (Toyota , Honda or Suzuki): ";
	cin>>req.brand;
	cout << "Please, enter a color of car (white , black or blue): ";
	cin >> req.color;
	cout << "Please, enter engine types of car (petrol, injector, diesel, gas, electric): ";
	cin>> req.engineType;
	cout << "Please, enter a car type (sedan , hatchback, cabriolet, crossover): ";
	cin >> req.carType;
	cout << "Please, enter a car cc (100-1299): ";
	cin >> req.carCC;


	int *matches = new int[num];
	bool searched = false;
	for (int j = 0; j < num; j++)
		matches[j] = 0;
	int i;
	for (i = 0; i < num; i++)
	{
		if (req.brand == c[i].brand)
		{
			matches[i]++;
		}
		if (req.color == c[i].color)
		{
			matches[i]++;
		}
		if (req.engineType == c[i].engineType)
		{
			matches[i]++;
		}
		if (req.carType == c[i].carType)
		{
			matches[i]++;
		}
		if (req.carCC == c[i].carCC)
		{
			matches[i]++;
		}
		if (matches[i] == 5)
		{
			searched = true;
			break;
		}
	}
	if (searched)
	{
		cout << "Exact match: ";
		DisplayCar(c[i]);
	}
	else
	{
		cout << "Possible cases:\n";
		for (int j = 0; j < num; j++)
		{
			if (matches[j] == 4 || matches[j] == 3)
			{
				DisplayCar(c[j]);
			}
		}
	}
	delete[] matches;
}

int main()
{
	const int num=10;
	Car *c = new Car[num];
	initialCars(c,num);
	cout << "Shop has buys this cars:\n";
	for (int i = 0; i < num; i++)
	{
		DisplayCar(c[i]);
	}
	requireCar(c, num);
	delete[] c;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog