create a class names as car with data member as carcompany,cost of car,number of airbags.create a file and the write a array of object into file.search the data by carcompany from the store file and display the result.
#include <iostream>
#include <fstream>
using namespace std;
class Car
{
string _nameOfCarCompany;
int _airbagsNumber;
public:
Car(){};
Car(string name, int number): _nameOfCarCompany(name), _airbagsNumber(number){}
string getNameOfCarCompany()
{
return _nameOfCarCompany;
}
int getAirbagsNumber()
{
return _airbagsNumber;
}
void setNameOfCarCompany(string name)
{
_nameOfCarCompany = name;
}
void setAirbagsNumber(int number)
{
_airbagsNumber = number;
}
};
int main()
{
Car *cars[3];
int i;
string name = "";
int number;
for(i = 0; i < 3; ++i)
{
cout << "Enter the name of car company: ";
cin >> name;
cout << "Enter the number of airbags: ";
cin >> number;
cars[i] = new Car(name, number);
}
fstream fs;
string buf = "";
int numBuf;
fs.open("cars.txt", fstream::out | fstream::in | fstream::app);
if(fs.is_open())
{
i = 0;
while(!fs.eof())
{
buf = cars[i]->getNameOfCarCompany();
fs << buf << "\n";
numBuf = cars[i]->getAirbagsNumber();
fs << numBuf << "\n";
i++;
}
}
else
cout << "File haven't been opened:(" << endl;
fs.close();
string carToFind = "";
cout << "Enter the name of car company you want to find: ";
cin >> carToFind;
fs.open("cars.txt", fstream::in | fstream::out | fstream::app);
if(fs.is_open())
{
i = 0;
while(!fs.eof())
{
buf = "";
numBuf = 0;
fs >> buf;
cars[i]->setNameOfCarCompany(buf);
fs >> numBuf;
cars[i]->setAirbagsNumber(numBuf);
i++;
}
}
else
cout << "File haven't been opened:(" << endl;
fs.close();
for(i = 0; i < 3; ++i)
{
if(cars[i]->getNameOfCarCompany() == carToFind)
{
cout << "Car you was looking for: " << endl;
cout << "Name of car company: " << cars[i]->getNameOfCarCompany() << endl;
cout << "Number of airbags: " << cars[i]->getAirbagsNumber() << endl;
}
}
return 0;
}
Comments
Leave a comment