#include #include #include #include "vehicle.h" using namespace std; typedef list VehiclePointerList; void openInputFile(ifstream& infile); void fillListFromFile(ifstream& infile, VehiclePointerList& vl); void printVehicleList(VehiclePointerList& vl); int main() { list vl; ifstream infile; openInputFile(infile); fillListFromFile(infile, vl); printVehicleList(vl); return 0; } void printVehicleList(VehiclePointerList& vl) { for (VehiclePointerList::iterator i = vl.begin(); i != vl.end(); i++) (*i)->print(); } void openInputFile(ifstream& infile) { const int failLimit = 4; char infileName[256]; int failCount = 0; do { cout << "Enter the name of the input file: "; cin >> infileName; infile.clear(); infile.open(infileName); if (infile.fail()) { cout << "The file whose name you've entered can't be opened. " << endl; failCount++; if (failCount > failLimit) { cout << "The program is terminating." << endl; exit(1); } } } while (infile.fail()); } void fillListFromFile(ifstream& infile, VehiclePointerList& vl) { char vehicleType; int vin, modelYear, doors, passCap, emptyWeight, load; while (infile >> vehicleType) { if (tolower(vehicleType) == 'c') { infile >> vin >> modelYear >> doors >> passCap; vl.push_back(new Car(vin, modelYear, doors, passCap)); } else if (tolower(vehicleType) == 't') { infile >> vin >> modelYear >> emptyWeight >> load; vl.push_back(new Truck(vin, modelYear, emptyWeight, load)); } else { cout << "Unknown vehicle type encountered: " << vehicleType << endl; exit(1); } } }