Answer to Question #180398 in C++ for DHARMVEER SINGH

Question #180398

create a class named as car with data members as car

company, cost of carnumber of airbags. Create a file and write the array of objects into file. Search the data by car company from the stored file and display the result


1
Expert's answer
2021-04-11T16:41:49-0400
#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
class Car
{
public:
	Car() {}
	Car(string carCompany_, double cost);
	string GetCompany() { return carCompany; }
	double GetCost() { return cost; }
private:
	string carCompany;
	double cost;
};
 
Car::Car(string CarComp, double cost_) : carCompany(CarComp), cost(cost_)
{
 
}
 
int main()
{
 
	Car Garage[5];
	Garage[0] = Car("BMW", 15000);
	Garage[1] = Car("AUDI", 25000);
	Garage[2] = Car("VOLVO", 35000);
	Garage[3] = Car("WV", 45000);
	Garage[4] = Car("FORD", 55000);
	ofstream fout;
	fout.open("Test.txt");
	for (int i = 0; i < 5; i++)
	{
		if (i == 4)
		{
			fout << Garage[i].GetCompany() << " " << Garage[i].GetCost();
			break;
		}
		fout << Garage[i].GetCompany() << " " << Garage[i].GetCost() << " ";
	}
	fout.close();
	ifstream fin;
	fin.open("Test.txt");
	string search = "FORD";
	string temp;
	double tempCost;
	while (!fin.eof())
	{
		fin >> temp >> tempCost;
		if (temp == search)
		{
			cout << temp << " " << tempCost << endl;
		}
	}
 
	return 0;
}

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
APPROVED BY CLIENTS