Search code examples
carrayspointersdynamic-arrays

Dynamic array class won't print


#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cstddef>

class ArrayList
{
    public:
        ArrayList();
        void expand();
        void store(std::string x); 
        void display_size();
        friend std::ostream& operator<<(std::ostream& os, const ArrayList &arr);
        void clean();
        ArrayList(const ArrayList &arr); 
        ~ArrayList();
        void operator=(const ArrayList& arr);
    private:
        int size; //total size of your dynamic array
        int max; //double the size of the array.
        int free_space; //total size of unused space in your dynamic array
        char *array; //a pointer used to create a dynamic array
};


ArrayList::ArrayList()
{
    size = 2;
    free_space = 1;
    array = new char[2]();
    array[0] = ' ';
    array[1] = '\0';
    max = size;

}

void ArrayList::expand()
{

    max = size + size;
    char *temp = new char[max];
    for( int i = 0; i < max; i++ )
    {
        array[i] = temp[i];
    }  
    free_space = free_space + size;
    delete [] array;
    array = temp;   
    size = max;
}

void ArrayList::store(std::string x) 
{
    int taken = max - free_space;
    int y = x.size();
    free_space = free_space - y;
    for(int i = 0; i < y; i++)
    {
        if(y >= size)
        {
            while(y >= size)
            {
                expand();
            }
        }
        else
        {
            array[i + taken] = x[i];
        }               
    }
}

std::ostream& operator<<(std::ostream& os, const ArrayList &arr)
{
    os << arr.array;
    return os;
}

void ArrayList::display_size()
{
    size = max;
    std::cout << "Array Content: ";
    std::cout << array;
    std::cout << std::endl;
    std::cout << "Remaining size: ";
    std::cout << free_space; 
    std::cout << std::endl;
}

void ArrayList::clean()
{
    int x = 0;
    for(int i = 0; i < size; i++)
    {
        if(array[i] == ' ')
        {
            x++;
        }
    }
    size = x;
}

ArrayList::ArrayList(const ArrayList &arr) 
{
    array = new char[size + 1];
    strcpy(array, arr.array);
}

ArrayList::~ArrayList()
{
    delete [] array;
}

void ArrayList::operator=(const ArrayList& arr)
{
    int new_length = strlen(arr.array);
    if(new_length > max)
    {
        delete [] array;
        max = new_length;
        array = new char[max + 1];
    }
    for(int i = 0; i < new_length; i++)
    {
        array[i] = arr.array[i];
    }
    array[new_length] = '\0';
}


int main()
{
    ArrayList x;
    std::string y;
    char ans;
    x.display_size();
    std::cout << "Please enter your string: ";
    std::cin >> y;
    x.store(y);
    x.display_size();// << std::endl;
    do
    {
        std::cout << "Please enter your string: ";
        std::cin >> y;
        x.store(y);
        x.display_size();

        std::cout << "Do you want to enter another string? (y/n) ";
        std::cin >> ans;
    }while(ans != 'n');
    return 0;
}

My question is regarding C++ dynamic arrays. I've created a class which creates a dynamic array.

I've posted my entire code it should be runnable.

The issue stems from the use of the store and expand functions.

store takes a string and puts each character into the array, if there isn't enough space it calls expand.

expand doubles the size of the array.

Array Content:  
Remaining size: 1
Please enter your string: h
Array Content:  h
Remaining size: 0
Please enter your string: ello
Array Content: 
Remaining size: 2
Do you want to enter another string? (y/n) n

In theory the output above should have returned "hello" however it hasn't returned anything, despite returning the 'h' earlier. I'm completely out of ideas on how to solve this.

EDIT:

I've since changed the functions according to the advice given to me:

void ArrayList::expand()
{
    max = size + size;
    char *temp = new char[max];
    for( int i = 0; i < max; i++ )
    {
        temp[i] = array[i];
    }  
    free_space = free_space + size;
    delete [] array;
    array = temp;   
    size = max;
}

void ArrayList::store(std::string x) 
{
    int taken = max - free_space;

    int y = x.size();
    free_space = free_space - y;
    for(int i = 0; i < y; i++)
    {
        if(free_space <= 0)
        {
            while(free_space <= 0)
            {
                expand();
            }
        }
        else
        {
            array[i+taken] = x[i]; //I'm cetain this didn't do anything
        }               
    }
}

I've solved the negative number issue detailed in the comments. The only issue now is printing the number.


Solution

  • This "for" loop in "expand" method:

    for( int i = 0; i < max; i++ )
    {
        array[i] = temp[i];
    }  
    

    should be replaced by:

    for( int i = 0; i < size; i++ )
    {
        temp[i] = array[i];
    }