title: Inserting an object into a C++ Sequential list
body: <p>For a school programming assignment I built an application that stores a list of objects in a sequential list object. The sequential list class has a method to insert a new object into the list, it checks first to see if the list already has the maximum number of entries allowed and if it does returns an error. For some reason I'm unable to insert a new object into the list (I keep getting the "Max list size exceeded" error) even though there aren't any entries in it to start. </p>
  
  <p>I ran it with a breakpoint to see if the size data member was increasing somehow but that doesn't seem to be the case here.</p>
  
  <p>Please ignore the poor code quality, still just learning... Feel free to make any recommendations :)</p>
  
  <p>Here's the main program: </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include "aseqlist.h"
  
  using namespace std;
  
  
  void PrintByGender (const SeqList&amp; L, char gender)
      {
          int size = L.ListSize();
  
          int count = 0;
  
          while (count &lt; size)
          {
  
          if (gender == L.GetData(count).getGender())
              {
                  L.GetData(count).PrintEmployee();
              }
              count++;
          }
  
  }
  
  int InList (const SeqList&amp; L, char *lname, Employee&amp; Emp)
  {
      int found = 0;
  
      Emp.setLast(lname);
  
      if (L.Find(Emp) == 1)
      {
          found = 1;
          Emp.PrintEmployee();
      }
  
      return found;
  
  }
  
  int main()
  {
      SeqList obj1;
  
      bool close = false;
      string choice = "";
  
      do
      {
  
          cout &lt;&lt; "Please choose what you would like to do: " &lt;&lt; "\n";
          cout &lt;&lt; "N = New record, D = Delete record, P = Print by gender, S = Search and E = Exit" &lt;&lt; "\n";
          cin &gt;&gt; choice;
          cin.ignore();
  
          if (choice == "n" || choice == "N")
          {
  
          string first, last;
          int age;
          char gen;
          double empNum;
  
          cout &lt;&lt; "First name: ";
          cin &gt;&gt; first;
  
          cout &lt;&lt; "Last name: ";
          cin &gt;&gt; last;
  
          cout &lt;&lt; "Age: ";
          cin &gt;&gt; age;
  
          cout &lt;&lt; "Gender ('M' Or 'F'): ";
          cin &gt;&gt; gen;
  
          cout &lt;&lt; "Employee Number: ";
          cin &gt;&gt; empNum;
  
          Employee newEmp;
  
          newEmp.ReadEmployee(first, last, age, gen, empNum);
  
          obj1.Insert(newEmp);
  
          }
  
  
          if (choice == "e" || choice == "E")
          {
  
              close = true;
  
          }
  
          if (choice == "p" || choice == "P")
          {
          char genderSearch;
          cout &lt;&lt; "Male = M, Female = F";
          cin &gt;&gt; genderSearch;
          cin.ignore();
  
          PrintByGender(obj1, genderSearch);
          }
  
          if (choice == "d" || choice == "D")
          {
              string last;
              cout &lt;&lt; "Which employee? (Enter Last Name): ";
              cin &gt;&gt; last;
              cin.ignore();
  
              Employee emp;
  
              emp.setLast(last);
  
              obj1.Delete(emp);
  
              cout &lt;&lt; "Deleted";
  
          }
  
          if (choice == "s" || choice == "S")
          {
  
          char lnameSearch;
          cout &lt;&lt; "Last Name?: ";
          cin &gt;&gt; lnameSearch;
          cin.ignore();
  
          Employee emp;
  
          char *ptrSearch;
  
          ptrSearch = &amp;lnameSearch;
  
          InList(obj1, ptrSearch, emp);
  
          if (emp.getFirst() != "")
          {
              emp.PrintEmployee();
  
          }
  
          }
  
      }
      while (close != true);
  
  };
  </code></pre>
  
  <p>And here's the header file for the class declarations: </p>
  
  <pre><code>#include &lt;iostream&gt; 
  
  using namespace std;
  const int MaxListSize = 6; 
  // You will need to change the typedef in the following line
  // from the data type int to Employee
  
  class Employee
  {
  
  public:
      Employee();
      Employee(string firstName, string lastName, int age, char gender, double employeeNumber);
      void ReadEmployee(string firstName, string lastName, int age, char gender, double employeeNumber);
      char getGender();
      string getFirst();
      void Employee::setLast(string lname);
      string getLast();
      void PrintEmployee();
  
  private:
          string LastName;
          string FirstName;
          int Age;
          char Gender;
          double EmployeeNumber;
  };
  
  typedef Employee DataType;
  class SeqList 
  { 
     private: 
        // list storage array and number of current list elements 
        DataType listitem[MaxListSize]; 
        int size;
  
     public: 
        // constructor 
        SeqList(void); 
  
        // list access methods 
        int ListSize(void) const; 
        int ListEmpty(void) const; 
        int Find (DataType&amp; item) const; 
        DataType GetData(int pos) const; 
  
        // list modification methods 
        void Insert(const DataType&amp; item); 
        void Delete(const DataType&amp; item); 
        DataType DeleteFront(void); 
        void ClearList(void); 
  }; 
  
  // Class Definition: 
  
  // constructor. set size to 0 
  SeqList::SeqList (void): size(6) 
  {} 
  
  // return number of elements in list 
  int SeqList::ListSize(void) const 
  { 
     return size; 
  } 
  
  // tests for an empty list 
  int SeqList::ListEmpty(void) const 
  { 
     return size == 0; 
  } 
  
  // clears list by setting size to 0 
  void SeqList::ClearList(void) 
  { 
     size = 0; 
  } 
  
  // Take item as key and search the list. return True if item 
  // is in the list and False otherwise. if found, 
  // assign the list element to the reference parameter item 
  
  
  bool  operator==(Employee A, Employee B)
  {
      bool isequal = false;
  
      if (A.getLast() == B.getLast())
          isequal = true;
  
      return isequal;
  }
  
  
  int SeqList::Find(DataType&amp; item) const 
  { 
     int i = 0; 
  
     if (ListEmpty()) 
        return 0;            // return False when list empty 
     while (i &lt; size &amp;&amp; !(item == listitem[i]))
        i++; 
     if (i &lt; size) 
     { 
        item = listitem[i];  // assign list element to item 
        return 1;            // return True 
     } 
     else 
        return 0;            // return False 
  } 
  
  // insert item at the rear of the list. terminate the program 
  // if the list size would exceed MaxListSize. 
  void SeqList::Insert(const DataType&amp; item) 
  { 
     // will an insertion exceed maximum list size allowed? 
     if (size+1 &gt; MaxListSize) 
     { 
        cout &lt;&lt; "Maximum list size exceeded" &lt;&lt; endl; 
        exit(1); 
     } 
  
     // index of rear is current value of size. insert at rear 
     listitem[size] = item; 
     size++;                 // increment list size 
  } 
  
  // search for item in the list and delete it if found 
  void SeqList::Delete(const DataType&amp; item) 
  { 
     int i = 0; 
  
     // search for item 
     while (i &lt; size &amp;&amp; !(item == listitem[i])) 
        i++; 
  
     if (i &lt; size)           // successful if i &lt; size 
     { 
        // shift the tail of the list to the left one position 
        while (i &lt; size-1) 
        { 
           listitem[i] = listitem[i+1]; 
           i++; 
        } 
        size--;              // decrement size 
     } 
  } 
  
  // delete element at front of list and return its value. 
  // terminate the program with an error message if the list is empty. 
  DataType SeqList::DeleteFront(void) 
  { 
     DataType frontItem; 
  
     // list is empty if size == 0 
     if (size == 0) 
     { 
        cout &lt;&lt; "Attempt to delete the front of an empty list!" &lt;&lt; endl; 
        exit(1); 
     } 
  
     frontItem = listitem[0];  // get value from position 0. 
     Delete(frontItem);        // delete the first item and shift terms 
     return frontItem;         // return the original value 
  } 
  
  
  // return value at position pos in list. if pos is not valid 
  // list position, teminate program with an error message. 
  DataType SeqList::GetData(int pos) const 
  { 
     // terminate program if pos out of range 
     if (pos &lt; 0 || pos &gt;= size) 
     { 
        cout &lt;&lt; "pos is out of range!" &lt;&lt; endl; 
        exit(1); 
     } 
     return listitem[pos]; 
  }
  
  Employee::Employee()
  {
  
      FirstName = "";
      LastName = "";
      Age = 0;
      /*Gender = "";*/
      EmployeeNumber = 0;
  
  };
  
  Employee::Employee(string firstName, string lastName, int age, char gender, double employeeNumber)
  {
          FirstName = firstName;
          LastName = lastName;
          Age = age;
          Gender = gender;
          EmployeeNumber = employeeNumber;
  };
  
  void Employee::PrintEmployee()
  {
                  cout &lt;&lt; "First Name: " &lt;&lt; FirstName &lt;&lt; "\n";
                  cout &lt;&lt; "Last Name: " &lt;&lt; LastName &lt;&lt; "\n";
                  cout &lt;&lt; "Age: " &lt;&lt; Age &lt;&lt; "\n";
                  cout &lt;&lt; "Gender: " &lt;&lt; Gender &lt;&lt; "\n";
                  cout &lt;&lt; "Employee Number :" &lt;&lt; EmployeeNumber &lt;&lt; "\n" &lt;&lt; "\n";
  
  };
  
  void Employee::ReadEmployee(string firstName, string lastName, int age, char gender, double employeeNumber)
  {
          FirstName = firstName;
          LastName = lastName;
          Age = age;
          Gender = gender;
          EmployeeNumber = employeeNumber;
  };
  
  char Employee::getGender()
  {
  
      return Gender;
  }
  string Employee::getFirst()
  {
  return FirstName;
  }
  string Employee::getLast()
  {
      return LastName;
  }
  void Employee::setLast(string lname)
  {
      LastName = lname;
  }
  </code></pre>