In summary, you have the following version of
analyze_train
:
#include#include #include const double pi = 3.14159; // Box and cylinder class definitions go here class railroad_car { public: char *serial_number; // Constructors: railroad_car ( ) {++counter;} railroad_car (char *input_buffer) { ++counter; serial_number = new char[strlen(input_buffer) + 1]; strcpy (serial_number, input_buffer); } // Destructor: virtual ~railroad_car ( ) { cout << "Destroying a railroad car; " << --counter << " remain." << endl; delete [ ] serial_number; } // Other: virtual char* short_name ( ) {return "rrc";} virtual double capacity ( ) {return 0.0;} private: static int counter; }; int railroad_car::counter = 0; // Other railroad-car class definitions go here railroad_car *train[100]; char input_buffer[100]; enum {eng_code = 'E', box_code = 'B', tnk_code = 'T', cab_code = 'C'}; char extract_car_code (char *input_buffer) {return input_buffer[4];} void do_analysis (ifstream& car_stream) { int n, car_count; for (car_count = 0; car_stream >> input_buffer; ++car_count) { delete train[car_count]; switch (extract_car_code (input_buffer)) { case eng_code: train[car_count] = new engine (input_buffer); break; case box_code: train[car_count] = new box_car (input_buffer); break; // Entries for tank cars and cabooses } } car_stream.close ( ); for (n = 0; n < car_count; ++n) cout << train[n] -> serial_number << " " << train[n] -> short_name ( ) << " " << train[n] -> capacity ( ) << endl; } main ( ) { // Read file name and call analysis function repeatedly: while (1) { cout << "Please type the name of a train file." << endl << ": "; cin >> input_buffer; // Open the input file: ifstream car_stream (input_buffer); // Give up if the input file does not exist: if (!car_stream) {cout << "No such file." << endl; return 0;} // Analyze: do_analysis (car_stream); // Close: car_stream.close ( ); } }