I'm having trouble with the (dynamic_cast<Truck*>(vptr[k])) -> DOTLicense() part.
If my numOfVehicles = 1 it works fine, but if numOfVehicles > 1 I get core dump error. I can remove the dynamic_cast statement and it works for numOfVehicles > 1. Any help or advise on what I'm doing wrong is appreciated.
int main()
{
const size_t maxNumOfVehicles(100);
const size_t maxSnSize(20);
const size_t maxDotSize(20);
size_t numOfVehicles;
std::cout << "Tracker started..." << std::endl;
// get the number of vehicles
std::cin >> numOfVehicles;
// Create an array of vehicle pointers
Vehicle* vptr[maxNumOfVehicles];
VehicleType v; // used to return an enum value
char sn [maxSnSize];
unsigned int passengerCap;
char dotLicense[maxDotSize];
float d1;
float d2;
float d3;
while((numOfVehicles != 0) && (numOfVehicles < maxNumOfVehicles)) // Begin
{
// Process the vehicle segment
for(size_t i=0;i<numOfVehicles;++i) // Begin first for loop
{
// get the vehicle serial number and check it
std::cin >> std::setw(maxSnSize) >> sn;
v = Vehicle::SnDecode(sn);
if(v == badSn) break; // don't process - can't make vehicle object
// get the passenger capacity
std::cin >> passengerCap;
switch(v) // Begin switch
{
case vehicle:
vptr[i] = new Vehicle(sn, passengerCap);
break;
case car:
vptr[i] = new Car(sn, passengerCap);
break;
case truck:
std::cin >> std::setw(maxDotSize) >> dotLicense;
vptr[i] = new Truck(sn, passengerCap, dotLicense);
break;
case van:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
std::cin >> d3;
vptr[i] = new Van(sn, passengerCap, dotLicense, d1, d2, d3);
break;
case tanker:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
vptr[i] = new Tanker(sn, passengerCap, dotLicense, d1, d2);
break;
case flatbed:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
vptr[i] = new Flatbed(sn, passengerCap, dotLicense, d1, d2);
break;
default:
std::cerr << "**Error: bad serial number passed to decision logic\n";
break;
} // end switch
} // end first for loop
// Send Report to Std. Output
std::cout << "Type Pass Cap Load Cap DOT License Serial Number\n";
std::cout << "---- -------- -------- ----------- -------------\n";
for(size_t k=0;k<numOfVehicles;++k) // begin second for loop
{
if(truck <= v) // begin if statement
{
std::cout << " " << vptr[k] -> ShortName() << " " << vptr[k] -> PassengerCapacity() << " " << vptr[k] -> LoadCapacity() << " " << (dynamic_cast<Truck*>(vptr[k])) -> DOTLicense() << " " << vptr[k] -> SerialNumber() << "\n";
}
else
{
std::cout << " " << vptr[k] -> ShortName() << " " << vptr[k] -> PassengerCapacity() << " " << vptr[k] -> LoadCapacity() << " (NA) " << vptr[k] -> SerialNumber() << "\n";
} // end if statement
delete vptr[k];
} // end second for loop
// Finished processing vehicle segment. Wait for next segment.
std::cout << "Tracker started..." << std::endl;
std::cin >> numOfVehicles;
} // end while loop
// Bye Bye message
std::cout << "...Thank you for using Tracker.\n";
return 0;
}