In: Computer Science
Modify the program so that, rather than printing data directly to the screen, it will read the data into an array of car structs and then print out the contents of the array.
#include "car.h" #include <iostream> #include <iomanip> #include <fstream> #include <string>
Car GetCar(ifstream& dataIn);
// Pre: File dataIn has been opened.
// Post: The fields of car are read from file dataIn.
void WriteCar(ofstream& dataOut, Car car);
// Pre: File dataOut has been opened.
// Post: The fields of car are written on file dataOut,
// appropriately labeled.
int main ()
{
Car car;
ifstream dataIn;
ofstream dataOut;
dataIn.open("cars.dat");
dataOut.open("cars.out");
cout << fixed << showpoint;
car = GetCar(dataIn);
while (dataIn)
{
car.price = car.price * 1.10f;
WriteCar(dataOut, car);
car = GetCar(dataIn);
}
return 0;
}
//*****************************************************
Car GetCar(ifstream& dataIn)
{
Car car;
dataIn >> car.customer;
dataIn >> car.price >> car.purchased.day
>> car.purchased.month >> car.purchased.year;
dataIn.ignore(2, '\n');
return car;
}
//*****************************************************
void WriteCar(ofstream& dataOut, Car car)
{
dataOut << "Customer: " << car.customer << endl
<< "Price: " << car.price << endl
<< "Purchased:" << car.purchased.day << "/"
<< car.purchased.month << "/"
<< car.purchased.year << endl;
}
.h file
#include <string>
struct PersonType {
std::string firstname;
std::string lastname;
};
struct Date
{
int month;
int day;
int year;
};
struct Car
{
float price;
Date purchased;
PersonType customer;
};
Answer:
Here is the modified code :
#include "car.h
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
Car GetCar(ifstream& dataIn);
// Pre: File dataIn has been opened. //
Post: The fields of car are read from file dataIn. void WriteCar(ofstream& dataOut, Car car);
// Pre: File dataOut has been opened. //
Post: The fields of car are written on file dataOut;
// appropriately labeled.
int main ()
{
Car car;
ifstream dataIn;
ofstream dataOut;
dataIn.open("cars.dat");
dataOut.open("cars.out");
cout << fixed << showpoint; car = GetCar(dataIn);
while (dataIn)
{
car.price = car.price * 1.10f;
WriteCar(dataOut, car);
car = GetCar(dataIn); } return 0;
}
//***************************************************** Car GetCar(ifstream& dataIn)
{
Car car;
Cout<<"enter range of i;
Cin>>I; For(i=0;i<i;i++)
{
dataIn >> car.customer[i];
dataIn >> car.price[i] >> car.purchased.day[i] >> car.purchased.month.[i] >> car.purchased.year[i];
}
dataIn.ignore(2, '\n');
return car;
}
//***************************************************** void WriteCar(ofstream& dataOut, Car car)
{
dataOut << "Customer: " << car.customer << endl << "Price: " << car.price << endl << "Purchased:" << car.purchased.day << "/" << car.purchased.month << "/" << car.purchased.year << endl;
}
.h file
#include <string>
struct PersonType
{
std::string firstname;
std::string lastname;
};
struct Date
{
int month;
int day;
int year;
};
struct Car
{
float price[50];
Date purchased[50];
PersonType customer[50];
};