I am trying to write function to add objects name Hotel to dynamically allocated array. Problem is, while my code can add the first one, it fails to add anything further than that. Here is the code responsible for adding new objects.
void HotelReservationSystem::addHotel( const std::string name, const int numFloors, const int *numRooms)
{
if ( hotelNum == 0 && hotels == NULL){
hotels = new Hotel[1];
Hotel hotelA ( name, numFloors, numRooms);
hotels[0] = hotelA;
hotelNum++;
std::cout << "Hotel " << name << " is added." << std::endl;
return;
}
for (int x = 0; x < hotelNum; x++){
if ( name == hotels[x].getName())
std::cout << "\n" << "Hotel " << name << " already exists." << std::endl;
return;
}
Hotel* temp = new Hotel[hotelNum+1];
for ( int x = 0; x < hotelNum; x++){
temp[x] = hotels[x];
}
temp[hotelNum] = Hotel ( name, numFloors, numRooms);
delete [] hotels;
hotels = temp;
hotelNum++;
std::cout << "Hotel " << name << " is added." << std::endl;
}
So far i cant detect anything wrong with this code.
std::vector. You're almost certainly going to make mistakes doing that, unless you're considerably smarter than me.std::vector-- this will shift your focus from trying to do this memory management yourself to actually developing and finishing your program (the Hotel reservation System).