#include "vehicle.h" #include #include #include #include #include using namespace std; const int MAXVEHICLES = 50; void openInputFile(ifstream& infile); void fillArrayFromFile(ifstream& infile, Vehicle *vp[], int& length); void printArray(Vehicle *vp[], int length); int main() { Vehicle *vp[MAXVEHICLES]; ifstream infile; int length; openInputFile(infile); fillArrayFromFile(infile, vp, length); printArray(vp, length); return 0; } void printArray(Vehicle *vp[], int length) { for (int index = 0; index < length; index++) { vp[index]->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 fillArrayFromFile(ifstream& infile, Vehicle *vp[], int& length) { char vehicleType; int vin, modelYear, doors, passCap, emptyWeight, load; length = 0; while (length < MAXVEHICLES && infile >> vehicleType) { if (tolower(vehicleType) == 'c') { infile >> vin >> modelYear >> doors >> passCap; vp[length] = new Car(vin, modelYear, doors, passCap); } else if (tolower(vehicleType) == 't') { infile >> vin >> modelYear >> emptyWeight >> load; vp[length] = new Truck(vin, modelYear, emptyWeight, load); } else { cout << "Unknown vehicle type encountered: " << vehicleType << endl; exit(1); } length++; } }