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
#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;
}
Comments
Leave a comment