0

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.

4
  • 1
    grumble grumble Whats wrong is that your not just using vectors (now I'll actually read your code) Commented Jul 9, 2014 at 16:54
  • 4
    "So far i cant detect anything wrong" - you're juggling pointers rather than using std::vector. You're almost certainly going to make mistakes doing that, unless you're considerably smarter than me. Commented Jul 9, 2014 at 16:55
  • Hrm...My guess would be something in hotel. What is the exact error you are getting? Can we see Hotel? Commented Jul 9, 2014 at 16:57
  • 1
    Is your goal to build a Hotel Reservation System, or to get bogged down in memory management and trying to create home-made dynamic arrays? If it's the former, then use 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). Commented Jul 9, 2014 at 17:22

2 Answers 2

3
for (int x = 0; x < hotelNum; x++){
    if ( name == hotels[x].getName())
        std::cout << "\n" << "Hotel " << name << " already exists." << std::endl;
        return;
}

Here, the return is not part of the if statement. Your code will just return in the first iteration. You need to put braces around those two lines.

Of course, as the comments say, you shouldn't be doing memory management like this yourself. Use std::vector instead. Your function would become only a few lines.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, problem is vectors are not an option for me at the moment. Other than this seems to be it. I am kinda embarassed that i have missed that so thank you.
0

You don't seem to have any declaration for the variable "hotels".

1 Comment

We can assume it's a member of HotelReservationSystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.