[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

Link List container class question

Asked by anushan in C++ Programming Language

Tags: link, addinorder, list

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,string,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(string 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(RolodexCard* next_info)
{
    next = next_info;  
}

void RolodexCard::set_fname(string _fname)
{
    firstname = _fname;
}

void RolodexCard::set_lname(string _lname)
{
    lastname = _lname;
}

void RolodexCard::set_occup(string _occup)
{
    occupation = _occup;
}

void RolodexCard::set_addr(string _addr)  
{
    address = _addr;    
}

void RolodexCard::set_phone(string _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,_addr,_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(RolodexCard *newCard)
{
    if (tail == 0)
      head = newCard;
    else
      tail->set_next(newCard);

    tail=newCard;
}


void RolodexFile::AddAtHead(RolodexCard *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::DisplayRolodex()
{
    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(choice);
     system("clear");
   } while (flag != 0);
[+][-]12/07/03 03:04 PM, ID: 9893404Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C++ Programming Language
Tags: link, addinorder, list
Sign Up Now!
Solution Provided By: meow00
Participating Experts: 1
Solution Grade: A
 
[+][-]12/07/03 11:30 AM, ID: 9892719Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/07/03 11:56 AM, ID: 9892813Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/07/03 01:48 PM, ID: 9893137Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/07/03 01:52 PM, ID: 9893153Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/07/03 02:11 PM, ID: 9893218Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/07/03 05:43 PM, ID: 9894000Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92