Question
c++ array of pointers, exceptions and try catch block of nested if statements Need help with an exception class, currently have some errors such as
c++ array of pointers, exceptions and try catch block of nested if statements
Need help with an exception class, currently have some errors such as too many initializers for *Ship[0] and C++ forbids comparison between pointer and integer.
here's what I have in my main that i'm working on figuring out where I'm having issues:
const int ARRAY_SIZE = 6;
Ship *ships[ARRAY_SIZE];
//try catch block
//ship name and information
for (int loop = 0; loop < ARRAY_SIZE; loop++)
{
try
{
if(ships == 0)
{
//ship - name, year, speed
Ship *ships[0] = {new Ship("name of ship", "2018", 17)};
}
if(ships == 1)
{
//cruiseship - name, year, speed, max passengers, decks
ShipArray *ships[1] = new Cruiseship("cruiseship", "2015" ,18, 3000, 26);
}
if (ships == 2)
{
//cargoship - name, year, speed, max tonnage
ShipArray *ships[2] = new Cargoship("Cargo ship", "1700", 23, 2896);
}
if(ships == 3)
{
//ship - name, year, max speed
ShipArray *ships[3] = new Ship("Ship", "1980", 14);
}
if(ships == 4)
{
//cruiseship - name, year, speed, max passengers, decks
ShipArray *ships[4] = new Cruiseship("Cruiseship 2", "2020" , 23, 50000, 80);
}
if (ships == 5)
{
//cargoship - name, year, speed, max tonnage
ShipArray *ships[5] = new Cargoship("Cargo 2", "2020", 17, 877874);
}
}
catch(Cruiseship::NegativeDecks exception)
{
cout << "Error: " << exception.getValue() <<
" is an invalid number of decks for " <<
shipName << "!" << endl;
}
catch(Cruiseship::NegativePassengers exception)
{
cout << "Error: " << exception.getValue() <<
" is an invalid number of passengers for " <<
shipName << "!" << endl;
}
catch(Ship::NegativeSpeed exception)
{
cout << "Error: " << exception.getValue() <<
" is an invalid speed for " <<
shipName << "!" << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started