First of all thanx to every1 who helped me in my last prob. There were some good tips and to those I listened to I changed the prog. Anyway this is a new prob.
Pls I know some ppl are against me using C-strings but I really I'm adopting this style of programming to try and learn the workings of C++ and make them it sink in hence the difficult way. Also it's my first OOP program. Anyway here goes:
The Class:
Where I think the problem stems from though it seems to be working fine:Code:class dbase { static int populus; char *name; int age; int phone; dbase *link; public: dbase() {}; // constructor dbase(char *nm, int ag, int telph); ~dbase(); dbase(const dbase &x); // copy constructor <======= This is new void set_details(char *nm, int ag, int telph); void show_details(); //operator declarations bool operator==(dbase op2); dbase operator=(dbase *op2); friend void initialiseList(dbase &list); friend bool listIsEmpty(dbase &list); friend void displayList(dbase &list); friend void insertIntoList(dbase &list, char *name, int age, int number); friend bool searchAndDisplay(dbase &list, char *name); friend int countEntriesInList(dbase &list); friend bool deleteFromList(dbase &list, char *name, int number); }; //end of class
The function that uses it:Code:dbase dbase::operator=(dbase *op2){ name = new char[(strlen(op2->name)+1)]; strcpy(name, op2->name); age = op2->age; phone = op2->phone; link = op2->link; return *this; }
Main itself:Code:bool deleteFromList(dbase &list, char *name, int number) { dbase *iterator, *key; iterator = &list; if ( (strcmp(iterator->name, name) == 0) && (iterator->phone == number) ) { list = list.link; return true; } else { while((iterator->link != NULL) && ((strcmp(iterator->link->name, name) !=0) && (iterator->link->phone != number)) ) iterator = iterator->link; if( (strcmp(iterator->link->name, name) ==0 ) && (iterator->link->phone == number)) { key = iterator->link; iterator->link = key->link; key->link = NULL; key->~dbase() ; return true; } }// end of 1st else return false; }
The problem is that when I call display list for the second time there seems to be only 1 object left as it seems the rest have been deleted. Now this only happens when I try to delete from the top of the list. I know the way I'm deleting from the top is odd, but this is due to my last problem which I found is due to the fact I was trying to change what the reference variable was pointing to as opposed to it's contents.Code:int main() { dbase list; //global declaration initialiseList(list); if (listIsEmpty(list) == true) cout<<"List is Truly empty.\n\n"; getchar() ; insertIntoList(list, "Bobo",2, 345); insertIntoList(list, "Cat",3, 587); insertIntoList(list, "Adt",5, 757); insertIntoList(list, "Doc",8, 434); insertIntoList(list, "Cot",4, 328); displayList(list); if (deleteFromList(list, "Adt", 757) == false) {cout<<"\n>>>>Unable to delete.<<<<\n";} cout<<countEntriesInList(list)<<" is the number of objects that currently exist.\n"; displayList(list); return 0; }
Shout out to cyberCloWn. This is mah 1st program in C++ and I'm also Using a Herbert Schildt's book, pretty damn good eh? (Mine's called "The Complete C++ Reference")
Again sorry for the longs posts.![]()



LinkBack URL
About LinkBacks




, for the want of a better word, BAFFLED. Would you please enlighten me as to why, because truly this only happens whe I delete from the top of the list and since I use the = constructor I created I was expecting the first two elements to be the same. seeing as there hasn't been any attempt to call the destructors unless it enters the else condition. But it shouldn't if I'm deleting from the top. right?
I've been found out. Guilty as charged. 'm I that obvious?