c++ - Function removes too much -
i'm doing phone registry , in need able add, remove , show phones on stock. i've made possible add in phones whenever add let's 3 phones , remove second 1 both third , second phone deleted , don't understand why.
this cellphonehandler.h file:
#ifndef cellphonehandler_h #define cellphonehandler_h #include "cellphone.h" class cellphonehandler { private: cellphone **phone; int nrofphones; int priceofphone; int stockcapacity; int nrofphonesinarr; public: cellphonehandler(); ~cellphonehandler(); void addphone(string brand, int nrof, int price); bool removephonefromstock(string name, int nrof); int getnrofphones() const; int getnrofphonesinarr() const; int getprice() const; void getphonesasstring(string arr[], int nrof, int priceofphone) const; }; #endif // !cellphonehandler_h
this cellphonehandler.cpp file.
#include "cellphonehandler.h" cellphonehandler::cellphonehandler() { this->phone = nullptr; this->nrofphones = 0; this->priceofphone = 0; this->stockcapacity = 0; this->nrofphonesinarr = 0; } cellphonehandler::~cellphonehandler() { (int = 0; < nrofphonesinarr; i++) { delete phone[i]; } delete[] phone; } void cellphonehandler::addphone(string brand, int nrof, int price) { if (stockcapacity < nrofphonesinarr + 1) { cellphone ** temparray = new cellphone*[this->nrofphonesinarr + 1]; (int = 0; < nrofphonesinarr; i++) { temparray[i] = this->phone[i]; } delete[] this->phone; this->phone = temparray; this->phone[this->nrofphonesinarr] = new cellphone(brand, nrof, price); this->nrofphonesinarr++; //this->stockcapacity++; } } bool cellphonehandler::removephonefromstock(string name, int nrof) { bool phonefound = false; int index = nrofphonesinarr; (int = 0; < nrofphonesinarr; i++) { if (this->phone[i]->getbrand() == name); { index = i; phonefound = true; this->nrofphonesinarr--; } } if (phonefound == true) { delete phone[index]; phone[index] = nullptr; } return phonefound; } int cellphonehandler::getnrofphones() const { return this->nrofphones; } int cellphonehandler::getnrofphonesinarr() const { return this->nrofphonesinarr; } int cellphonehandler::getprice() const { return this->priceofphone; } void cellphonehandler::getphonesasstring(string arr[], int nrof, int priceofphone) const { (int = 0; < nrof; i++) { arr[i] = this->phone[i]->tostring(); } }
the problem caused unwanted ;
.
if (this->phone[i]->getbrand() == name); // if ends here.
the next block executed items.
{ index = i; phonefound = true; this->nrofphonesinarr--; }
remove ;
in if
line.
Comments
Post a Comment