Jump to content

need help c++ expanding dynamic array size in class

Hi, I could use some help figuring out what i'm doing wrong with this code. I'm trying to increase the size of my dynamic array in a class.

 

Here's part of the header file with the class

template<class ItemType>
class Container
{
private:
    int DEFAULT_SIZE = 10;
    ItemType* items; // array of container items
    int itemCount;                    // current count of container items
    int maxItems;                     // max capacity of the container
    
public:
    Container();//constructor
    ~Container();// destructor
    int increaseSize();//expand array size
    bool add(const ItemType& newEntry); // add new value to array

here is my constructor and methods that are called:

//default constructor
template<class ItemType>
Container<ItemType>::Container(): itemCount(0), maxItems(DEFAULT_SIZE) {
    items = new ItemType[DEFAULT_SIZE];
}// end default constructor
  
  // add to container
template<class ItemType>
bool Container<ItemType>::add(const ItemType& newEntry) {
    bool hasRoomToAdd = (itemCount < maxItems);
    if (hasRoomToAdd) {
        items[itemCount] = newEntry;
        itemCount++;
    } // end if
    return hasRoomToAdd;
} // end add
                                               
// increase size of array
template<class ItemType>
int Container<ItemType>::increaseSize(){
      int i = 0;

    while (std::cin >> items[i]){
        std::cout << "items: " << items[i] << "\n";
        i++;
        if ( i >= DEFAULT_SIZE){
            DEFAULT_SIZE = DEFAULT_SIZE * 2; // double previous size
            int *deleteMe = new int[DEFAULT_SIZE]; // create new bigger array
            for (int j = 0; j < i; j++)
                deleteMe[j] = items[j]; // copy values to new array
            delete[] items; // free memory of old array
            items = deleteMe; // point to new array
        }
    }
    return DEFAULT_SIZE;
}// end increaseSize

I call the add method and it works fine, but if my array is full user will get a message that the array is full in my main.cpp. The increaseSize() method should double the current array and copy all existing elements into a new array then delete the old array. When I run the code it gets stuck on the while statement. I can't even find anything in the debugger. If you need more from the code let me know. All input is type int, haven't experimented with double or anything else yet. 

Link to post
Share on other sites

41 minutes ago, Pachuca said:

I call the add method and it works fine, but if my array is full user will get a message that the array is full in my main.cpp. The increaseSize() method should double the current array and copy all existing elements into a new array then delete the old array. When I run the code it gets stuck on the while statement. I can't even find anything in the debugger. If you need more from the code let me know. All input is type int, haven't experimented with double or anything else yet. 

Your add method never calls increaseSize. All it does is say "is there room, if so add the item, otherwise, return and exit". 

Your specific error is because arrays are zero indexed, and I would assume that itemCount is 1 when the first item is entered, instead of 0.

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

59 minutes ago, straight_stewie said:

Your add method never calls increaseSize. All it does is say "is there room, if so add the item, otherwise, return and exit". 

Your specific error is because arrays are zero indexed, and I would assume that itemCount is 1 when the first item is entered, instead of 0.


 

itemCount(0) is set to 0 in the constructor. If it was an index issue I wouldn't be able to use add method either. I have an if statement that calls both methods in main.cpp

 

if (!objectInt.isFull()) {
                        cout << "Enter a value to add:\n";
                        cin >> contents[index];
                        objectInt.add(contents[index]);
                        cout << "New data: \n";
                        objectInt.displayContents();
                    } else {
                        objectInt.increaseSize();
                        cout << "Enter a value to add:\n";
                        cin >> contents[index];
                        objectInt.add(contents[index]);
                        cout << "New data: \n";
                        objectInt.displayContents();
                    }

 

Link to post
Share on other sites

5 hours ago, Unimportant said:

Why is 'IncreaseSize' even reading input from cin in the first place, and overwriting your container items ?

Just a way to keep the size/count of the array to use later in the for loop too. Don't worry about it, I already fixed the code and got it working. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×