Hi Experts,
I am almost done with my assignment...but have a few issues:
1. When I modify a card using "ChangeCard" function....and enter a last name that doesn't exist....I get a "Segmentation fault" error ??? Can someone clarify and help me out here
2. The program works fine as long as I add a couple of records...and also delete all of them.
But once I try to re-add records after deleting all of the records.....and then try to display the re-added records...It says "NO RECORDS FOUND">>>Am I doing somethign wrong here??
Please help ??
#include <iostream>
#include <string>
using namespace std;
class RolodexCard {
private:
string firstname;
string lastname;
string occupation;
string address;
string phone_number;
RolodexCard * next;
public:
RolodexCard(string,string,
string,str
ing,string
);
RolodexCard();
void set_next(RolodexCard *);
void set_fname(string);
void set_lname(string);
void set_occup(string);
void set_addr(string);
void set_phone(string);
RolodexCard * get_next();
string get_lname();
bool operator > (RolodexCard& op1);
void print_output();
};
RolodexCard::RolodexCard(s
tring fname, string lname, string occup, string addr,
string phone_num)
{
firstname = fname;
lastname = lname;
occupation = occup;
address = addr;
phone_number = phone_num;
next = 0;
}
RolodexCard::RolodexCard()
{
next = 0;
}
void RolodexCard::set_next(Rolo
dexCard* next_info)
{
next = next_info;
}
void RolodexCard::set_fname(str
ing _fname)
{
firstname = _fname;
}
void RolodexCard::set_lname(str
ing _lname)
{
lastname = _lname;
}
void RolodexCard::set_occup(str
ing _occup)
{
occupation = _occup;
}
void RolodexCard::set_addr(stri
ng _addr)
{
address = _addr;
}
void RolodexCard::set_phone(str
ing _phone)
{
phone_number = _phone;
}
RolodexCard * RolodexCard::get_next()
{
return next;
}
string RolodexCard::get_lname()
{
return lastname;
}
bool RolodexCard::operator > (RolodexCard & op1)
{
bool isGreat = false;
if( lastname > op1.lastname )
isGreat = true;
return isGreat;
}
void RolodexCard::print_output(
)
{
cout << "*************************
**********
"<<endl;
cout << "Firstname :" << " " << firstname << endl;
cout << "Lastname :" << " " << lastname << endl;
cout << "Occupation:" << " " << occupation << endl;
cout << "Address :" << " " << address << endl;
cout << "Phone :" << " " << phone_number << endl;
}
class RolodexFile
{
private:
RolodexCard * head;
RolodexCard * tail;
RolodexCard * currptr;
public:
void AddInOrder();
void AddAtHead(RolodexCard *newCard);
void AddAtTail(RolodexCard *newCard);
void DeleteCard();
void ChangeCard();
void DisplayRolodex();
void Erase();
int handle_choice(int);
RolodexFile(); // default CTOR
~RolodexFile(); // default DTOR
};
RolodexFile::RolodexFile()
{
head = 0;
tail = 0;
currptr = head;
}
RolodexFile::~RolodexFile(
)
{
Erase();
}
void RolodexFile::AddInOrder() // This is Insertion Sort
{
string _fname;string _lname;string _occup; string _addr; string _phone;
cout << "\nADDING A NEW CARD\n";
cout << "Firstname: ";
cin.ignore(80,'\n');
getline(cin,_fname);
cout << "Lastname: ";
getline(cin,_lname);
cout << "Occupation: ";
getline(cin,_occup);
cout << "Address: ";
getline(cin,_addr);
cout << "Phone number: ";
getline(cin,_phone);
RolodexCard * newCard = new RolodexCard(_fname,_lname,
_occup,_ad
dr,_phone)
;
RolodexCard * nextptr = head;
RolodexCard * lastptr = 0;
while(nextptr!=0 && (*newCard) > (*nextptr))
{
lastptr = nextptr;
nextptr = nextptr->get_next();
}
if (nextptr == 0) // hit end of list, AddAtTail, so even if it didn't enter the loop, it should atleast add at the Tail
{
AddAtTail(newCard);
}
else
{
if (lastptr == 0) // this means to add at the beginning
{
AddAtHead(newCard);
}
else // this means tp add at the middle
{
newCard->set_next(nextptr)
;
lastptr->set_next(newCard)
;
}
}
}
void RolodexFile::AddAtTail(Rol
odexCard *newCard)
{
if (tail == 0)
head = newCard;
else
tail->set_next(newCard);
tail=newCard;
}
void RolodexFile::AddAtHead(Rol
odexCard *newCard)
{
if (head==0)
tail = newCard;
newCard->set_next(head);
head = newCard;
}
void RolodexFile::DeleteCard()
{
string _lname; int i = 0;
RolodexCard * prevptr = 0; // initialize previous ptr to NULL
cout << "\nEnter the Lastname of the card that you want to delete: ";
cin.ignore(80,'\n');
getline(cin,_lname);
for(currptr = head; currptr != 0; currptr= currptr->get_next() )
{
if (currptr->get_lname() == _lname)
{
if (prevptr == 0)
head = currptr->get_next();
else
prevptr->set_next(currptr-
>get_next(
));
delete currptr;
i = 1;
}
else
prevptr = currptr;
}
if (i == 0)
{
cout << "\n\nNO MATCH FOUND FOR THE LASTNAME YOU ENTERED\n\n";
}
}
void RolodexFile::ChangeCard()
{
string _fname;string _lname;string _occup;string _addr;string _phone;
int choice; int flag = 0;
RolodexCard * temp = head;
cout << "\nEnter the lastname of the card you want to modify\n";
cin.ignore(80,'\n');
getline(cin,_lname);
while((temp->get_lname() != _lname) && temp != 0)
{
temp = temp->get_next();
}
if (temp != 0)
{
flag = 1;
cout << "\nCARD FOUND\n";
cout << "\nChoose one of the following\n";
cout << "0 - Change Firstname\n";
cout << "1 - Change Lastname\n";
cout << "2 - Change Occupation\n";
cout << "3 - Change Address\n";
cout << "4 - Change Phone number\n";
cout << "\nENTER CHOICE: ";
cin >> choice;
switch(choice)
{
case 0:
cout << "Enter the new Firstname: ";
cin.ignore(80,'\n');
getline(cin,_fname);
temp->set_fname(_fname);
break;
case 1:
cout << "Enter the new Lastname: ";
cin.ignore(80,'\n');
getline(cin,_lname);
temp->set_lname(_lname);
break;
case 2:
cout << "Enter the new Occupation: ";
cin.ignore(80,'\n');
getline(cin,_occup);
temp->set_occup(_occup);
break;
case 3:
cout << "Enter the new Address: ";
cin.ignore(80,'\n');
getline(cin,_addr);
temp->set_addr(_addr);
break;
case 4:
cout << "Enter the new Phone Number: ";
cin.ignore(80,'\n');
getline(cin,_phone);
temp->set_phone(_phone);
break;
default:
cout << "\nInvalid choice\n";
break;
}
}
if (flag == 0)
{
cout << "\n\nNO MATCH FOUND FOR THE LASTNAME YOU ENTERED\n\n";
}
}
void RolodexFile::Erase()
{
RolodexCard * temp = head;
currptr = head;
while(currptr)
{
currptr = currptr->get_next();
delete temp;
temp = currptr;
}
}
void RolodexFile::DisplayRolode
x()
{
currptr = head;
if(currptr != 0)
{
cout << "*************************
**********
**\n";
cout << "** Displaying the Rolodex Cards ***\n";
while (currptr)
{
currptr->print_output();
currptr = currptr->get_next();
}
}
else
cout << "\n\nEVERYTHING Erased NO LIST to PRINT ******* ";
currptr = head;
}
int RolodexFile::handle_choice
(int choice)
{
switch(choice)
{
case 0:
return 0;
case 1:
AddInOrder();
cout << "\n\nPress any key to continue....";
getchar();
return 1;
case 2:
DisplayRolodex();
cout << "\n\nPress any key to continue....";
getchar();
return 1;
case 3:
ChangeCard();
cout << "\n\nPress any key to continue....";
getchar();
return 1;
case 4:
DeleteCard();
cout << "\n\nPress any key to continue...";
getchar();
return 1;
default:
cout << "\n\nInvalid Choice !!";
cout << "\n\nPress any key to continue....";
getchar();
return 0;
}
}
void main()
{
int choice; int flag=1;
RolodexFile r_file;
do
{
cout << "\nWELCOME - ROLODEX CARDS\n";
cout << "*************************
****\n";
cout << "0 - Exit Program\n";
cout << "1 - Add a Card (will be sorted in order)\n";
cout << "2 - Display Rolodex\n";
cout << "3 - Search Rolodex by Lastname and Change\n";
cout << "4 - Delete a Card\n";
cout << " Enter Choice: ";
cin >> choice;
flag = r_file.handle_choice(choic
e);
system("clear");
} while (flag != 0);