Assembling all file-handling elements into a new program, you have the following:
#include#include #include const double pi = 3.14159; // 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) { // Declare various integer variables: int n, car_count; // Read serial number and create car class objects: for (car_count = 0; car_stream >> input_buffer; ++car_count) { // Construct new class object: 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; case tnk_code: train[car_count] = new tank_car (input_buffer); break; case cab_code: train[car_count] = new caboose (input_buffer); break; } } // Display report here } 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 ( ); } }