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:
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
Where I think the problem stems from though it seems to be working fine:
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;
}
The function that uses it:
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;
}
Main itself:
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;
}
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.
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.