-1

i've tried to add an element from class type to an empty dynamic array, but nothing happens, nor it changes the counter after adding one element. This is the function

void Bank::addCustomer(const Customer& newCustomer) {

    Customer* temp = new Customer[getCustomerCount()+1]; 

    for (int i = 0; i < getCustomerCount() + 1 ; ++i) {
        if (getCustomerCount() != 0) {
            temp[i] = customers[i];
        }
    }

    ++customerCount;
    setCustomerCount(customerCount);
    delete[] customers; 
    customers = temp;

    customers[customerCount] = newCustomer; 
    //log
}
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Shus
  • 3
  • 2
  • 3
    Please post a [mre]. – wohlstad May 26 '22 at 16:43
  • @Shushu At least this for loop for (int i = 0; i < getCustomerCount() + 1 ; ++i) { invokes undefined behavior. – Vlad from Moscow May 26 '22 at 16:46
  • @Shushu This if statement if (getCustomerCount() != 0) { does not make a sense. – Vlad from Moscow May 26 '22 at 16:47
  • @Shushu This assignment customers[customerCount] = newCustomer; again invokes undefined behavior. – Vlad from Moscow May 26 '22 at 16:47
  • @Shushu The function in whole does no make a sense because there are used the variable customerCount and the function getCustomerCount at the same time. So the question should be closed as senseless. – Vlad from Moscow May 26 '22 at 16:50
  • 2
    `std::vector` -- This solves all of the problems. If you say you can't use this, this is one reason why students drop using C++ and pick up Java or some other language. – PaulMcKenzie May 26 '22 at 17:00
  • 1
    as a side note, I strongly suggest you adopt a naming convention for member fields (`m_customerCount`, `_customerCount`, `customerCount_` are the common ones) – pm100 May 26 '22 at 17:15
  • Note how pm100's suggestions don't lead with an underscore and follow it with an uppercase letter. [That would be bad](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – user4581301 May 26 '22 at 17:51

3 Answers3

0

Your loop is exceeding the bounds of the old array if it is not empty. And your assignment of the newCustomer is exceeding the bounds of the new array.

Try this instead:

void Bank::addCustomer(const Customer& newCustomer) {

    int count = getCustomerCount(); 
    Customer* temp = new Customer[count + 1];

    for (int i = 0; i < count; ++i) {
        temp[i] = customers[i];
    }
    temp[count] = newCustomer; 

    delete[] customers; 
    customers = temp;

    ++customerCount;

    //log
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I'm guessing this is what you mean:

void addCustomer(const Customer& newCustomer) {
    int old_customer_count = getCustomerCount();
    int new_customer_count = old_customer_count + 1;

    Customer* temp = new Customer[new_customer_count];
    for (int i = 0; i < new_customer_count; ++i) {
        temp[i] = customers[i];
    }

    setCustomerCount(new_customer_count);
    delete[] customers;
    customers = temp;
    customers[old_customer_count] = newCustomer;
}

but as someone said, in real code you should an std::vector for the customers collection.

jwezorek
  • 8,592
  • 1
  • 29
  • 46
0

Try using vector for dynamic array. arry cann't be dynamic. push_back() function can be used for adding element to vector.