Exercise 1: Augment Car in program Cars with the following two members: sold A b
ID: 3656167 • Letter: E
Question
Exercise 1: Augment Car in program Cars with the following two members: sold A boolean variable soldDate If (sold), then soldDate contains the date of sale; otherwise soldDate is undefined Function GetCar should initialize sold to false. Write a function CarSold that takes variables of type Date and Car and records that the car has been sold and the date. Before invoking PrintCar, write the car owner's name on the screen and ask if the car has been resold. If it has, call function CarSold and then write the car to file dataSold rather than file dataOut. Exercise 2: Rewrite Car so that soldDate and the new owner's name are encapsulated into a struct member soldTo. If the car has been resold, prompt for and read the new owner's name. Run your program again using cars.dat.
Explanation / Answer
//exercise 1
#include<iostream>#include<fstream>#include<string>using namespace std;
struct Date { int month; int day; int year;};
struct Car { float price; Date purchased; string customer; bool sold; Date soldDate;};
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.
void CarSold(Date,Car);//records that the car has been sold and the date
int main () { Car car; char response; Date date;
ifstream dataIn; ofstream dataOut; ofstream dataSold; dataIn.open("cars.dat"); dataOut.open("cars.out"); dataSold.open("cars.sold"); cout << fixed << showpoint; car = GetCar(dataIn);
while (dataIn) { car.price = car.price * 1.10; cout<<"Car owner: "<<car.customer <<" Has the car been resold?(Y/N):"; cin>>response; if(response=='Y'||response=='y'){ do{ cout<<"Enter a valid sold date (MM DD YYYY): "; cin>>date.month>>date.day>>date.year; CarSold(date,car); }while((date.month>12)||(date.month<1)||(date.day<1)||(date.day>31)||(date.year<car.purchased.year)); WriteCar(dataSold,car); } else{ 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; car.sold=false;//initialize sold to false dataIn.ignore(2, ' '); 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; if(car.sold==true){ dataOut<<"Sold "<< car.soldDate.day << "/" << car.soldDate.month << "/" << car.soldDate.year << endl; } }
//***************************************************** void CarSold(Date soldDate,Car car){ car.sold=true; car.soldDate=soldDate; }//*****************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.