title: C++ Template Linked List storing Class Objects - Unhandled Exception Access Violation Reading Location
body: <p>I'm trying to implement a template linked list into a project I'm working on with a lot of classes. It works a bit like a map or set where there is key data and value data. I created it and tested it in a seperate project using ints and string values, and all the functions work inside it work, I even got it to work with serialization using the BOOST libraries.</p>
  
  <p>This is the entire class file for my list:</p>
  
  <pre><code>#ifndef LINKED_LIST_H
  #define LINKED_LIST_H
  
  #include &lt;boost\serialization\access.hpp&gt;
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  template&lt;typename Tkey, typename Tvalue&gt;
  class Node
  {
  public:
  Tkey keyData;
  Tvalue valueData;
  Node * next;
  Node()
  {   
      next = NULL;
  }
  Node(Tkey x, Tvalue y)
  {
      keyData = x;
      valueData = y;
      next = NULL;
  }
  Node(Tkey x, Tvalue y, Node * z)
  {
      keyData = x;
      valueData = y;
      next = z;
  }
  
  template&lt;class Archive&gt;
  void serialize(Archive &amp;ar, const unsigned int version)
  {
      ar &amp; keyData;
      ar &amp; valueData;
      ar &amp; next;
  }
  };
  
  template&lt;typename Tkey, typename Tvalue&gt;
  class LinkedList
  {
  Node&lt;Tkey, Tvalue&gt; *head;
  int count;
  public:
  LinkedList()
  {
      head = NULL;
      count = 0;
  }
  ~LinkedList()
  {
      Clear();
  }
  
  template&lt;class Archive&gt;
  void serialize(Archive &amp;ar, const unsigned int version)
  {
      ar &amp; head;
      ar &amp; count;
  }
  
  int Size()
  {
      return count;
  }
  
  bool Empty()
  {
      return (count == 0);
  }
  
  bool Add(Tkey key, Tvalue value)
  {
      Node&lt;Tkey, Tvalue&gt; *p;
      if(head == NULL)
      {
          head = new Node&lt;Tkey, Tvalue&gt; (key, value, NULL);
          count++;
          return true;
      }
      else
      {
          p = head;
          while(p-&gt;next !=NULL)
              p=p-&gt;next;
          p-&gt;next = new Node&lt;Tkey, Tvalue&gt; (key, value, NULL);
          count++;
          return true;
      }
      return false;
  }
  
  bool Remove(Tkey key)
  {
      Node&lt;Tkey, Tvalue&gt; *p;
  
      if (head-&gt;keyData == key)
      {
          head = head-&gt;next;
          return true;
      }
  
      p=head;
  
      while (p-&gt;next-&gt;keyData != key &amp;&amp; p-&gt;next-&gt;next != NULL)
      {
          p = p-&gt;next;
      }
  
      if (p-&gt;next-&gt;keyData == key &amp;&amp; p-&gt;next != NULL)
      {
          Node&lt;Tkey, Tvalue&gt; *temp;
          temp = p-&gt;next;
          p-&gt;next = temp-&gt;next;
          count--;
          delete temp;
          return true;
      }
  
      return false;
  }
  
  Tvalue Get(Tkey key)
  {
      Node&lt;Tkey, Tvalue&gt; *p;
  
      p = head;
  
      while (p-&gt;next != NULL)
      {
          if (p-&gt;keyData == key)
          {
              return p-&gt;valueData;
          }
          p = p-&gt;next;
      }
  
      return NULL;
  }
  
  Tvalue Find(int i)
  {
      int selection = 0;
  
      Node&lt;Tkey, Tvalue&gt; *p;
  
      p = head;
  
      while (selection != i &amp;&amp; p-&gt;next != NULL)
      {
          p = p-&gt;next;
          selection++;
      }
  
      if (p-&gt;next == NULL)
      {
          return 0;
      }
  
      return p-&gt;valueData;
  }
  
  Tkey Exists (Tvalue value)
  {
      Node&lt;Tkey, Tvalue&gt; *p;
  
      p = head;
  
      while (p-&gt;next != NULL)
      {
          if (p-&gt;valueData == value)
          {
              return p-&gt;keyData;
          }
      }
  
      return NULL;
  }
  
  void Clear()
  {
      Node&lt;Tkey,Tvalue&gt; * temp;
      while (head != 0)
      {
          temp = head;
          head = head-&gt;next;
          count--;
          delete temp;
      }
  }
  
  void Print(Tkey key)
  {
      Node&lt;Tkey, Tvalue&gt; * p;
      p = head;
      while(p-&gt;keyData != key &amp;&amp; p-&gt;next != NULL)
      {
          p = p-&gt;next;
      }
  
      if (p-&gt;keyData == key &amp;&amp; p-&gt;next != NULL)
      {
          cout &lt;&lt; p-&gt;keyData &lt;&lt; " " &lt;&lt; p-&gt;valueData &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "Data not found" &lt;&lt; endl;
      }
  }
  
  void PrintAll()
  {
      Node&lt;Tkey, Tvalue&gt; * p;
      p = head;
      while(p != NULL)
      {
          cout &lt;&lt; p-&gt;keyData &lt;&lt; " " &lt;&lt; p-&gt;valueData &lt;&lt; "\n";
          p = p-&gt;next;
      }
  }
  };
  #endif
  </code></pre>
  
  <p>To test it out in my project, I created a Linked List variable with an int and one of my classes, then tried to add and get the value data to modify a value.</p>
  
  <pre><code>LinkedList&lt;int,Customer *&gt; customerList;
  customerList.Add(1, new Customer("Jack Smith", "1 Adelade Road", "5678", "Male", 4,8,1999));
  customerList.Get(1)-&gt;AddAccount("9081", new JuniorCurrentAccount("100126764", 50, 25,12,2010));
  </code></pre>
  
  <p>However when I run it I get an unhandled exception at the Add method pointing at 
      if(head == NULL)</p>
  
  <p>The exception says "Access violation reading location"</p>
  
  <p>So far I have tried changing the NULLs to 0s when the list checks for Nodes but that didn't stop the exception, I have also tried including the headers for the class files but that caused a lot of errors that involving accessing variables within those classes.</p>
  
  <p>I believe something is wrong within the class itself, but I cannot find what is causing it. As I said, this class works fine with basic data types (ints, strings ect) but classes is what is causing the errors. Any help would be greatful!</p>
  
  <p>EDIT: Here is the code for the Customer Class .cpp file<br>
      #include "Customer.h"
      #include "Account.h"
      #include "CorportateSavings.h"
      #include "CurrentAccount.h"
      #include "JuniorCurrentAccount.h"
      #include "StudentSavings.h"
      #include "Mortgage.h"
      #include "BuyToLetMortgage.h"
      #include "DiscountMortgage.h"
      #include "FixedRateMortgage.h"
      #include 
      #include 
      #include 
      #include 
      #include </p>
  
  <pre><code>using namespace std;
  
  Customer::Customer(string Name, string Address, string TelephoneNo, string Gender, int DOBday, int DOBmonth, int DOByear) : name(Name), address(Address), telephoneNo(TelephoneNo), gender(Gender), DOB(DOBday,DOBmonth,DOByear)
  {
  parent = 0;
  }
  
  Customer::Customer(string Name, string Address, string TelephoneNo, string Gender, int DOBday, int DOBmonth, int DOByear, Customer *c) : name(Name), address(Address), telephoneNo(TelephoneNo), gender(Gender), DOB(DOBday,DOBmonth,DOByear)
  {
  parent = c;
  }
  
  Customer::~Customer(void)
  {
  
  }
  
  void Customer::printDetails()
  {
  //Prints out details of customer
  cout &lt;&lt; "Name       : " &lt;&lt; name &lt;&lt; endl;
  cout &lt;&lt; "Address        : " &lt;&lt; address &lt;&lt; endl;
  cout &lt;&lt; "Telephone No   : " &lt;&lt; telephoneNo &lt;&lt; endl;
  cout &lt;&lt; "Gender     : " &lt;&lt; gender &lt;&lt; endl;
  cout &lt;&lt; "Date of Birth  : ";
  printStringDOB();
  //If customer has a registered parent/guardian
  if (parent != 0)
      cout &lt;&lt; "Parent/Guardian Name: " &lt;&lt; parent-&gt;getName() &lt;&lt; endl;
  
  cout &lt;&lt; endl;
  }
  
  string Customer::getName()
  {
  return name;
  }
  
  void Customer::setName(string Name)
  {
  name = Name;
  }
  
  string Customer::getAddress()
  {
  return address;
  }
  
  void Customer::setAddress(string Address)
  {
  address = Address;
  }
  
  string Customer::getTelephoneNo()
  {
  return telephoneNo;
  }
  
  void Customer::setTelephoneNo(string TelephoneNo)
  {
  telephoneNo = TelephoneNo;
  }
  
  string Customer::getGender()
  {
  return gender;
  }
  
  void Customer::setGender(string Gender)
  {
  gender = Gender;
  }
  
  void Customer::printStringDOB()
  {
  DOB.printString(); 
  }
  
  double Customer::getDOB()
  {
  return DOB.juldays();
  }
  
  void Customer::setDOB(int day, int month, int year)
  {
  DOB.setDay(day);
  DOB.setMonth(month);
      DOB.setYear(year);
  }
  
  int Customer::getAge(int Day, int Month, int Year)
  {
  return DOB.DifferenceYear(Day, Month, Year);
  }
  
  void Customer::setParent(Customer * c)
  {
  parent = c;
  }
  
  Customer * Customer::getParent()
  {
  if (parent = 0)
  {
      return NULL;
  }
  else
  {
      return parent;
  }
  }
  
  void Customer::AccountManager()
  {
  int choice = AccountManagerChooser();
  cout &lt;&lt; endl;
  
  while (choice != 9)
  {
      switch (choice)
      {
      case 1:
          //Create a bank acount
          CreateAccountManager();
          choice = AccountManagerChooser();
          break;
      case 2:
          {
              //Use a bank account
              string pin;
  
              cout &lt;&lt; "Enter PIN: ";
              cin &gt;&gt; pin;
  
              UseAccount(pin);
              choice = AccountManagerChooser();
              break;
          }
      case 3:
          {
              //Remove a bank account
              char yn;
              cout &lt;&lt; "Enter PIN: ";
              string pin;
              cin &gt;&gt; pin;
              if (accountList.Get(pin) != NULL)
              {
                  cout &lt;&lt; "Are you sure? (Y/N) ";
                  cin &gt;&gt; yn;
                  if (yn == 'y' || yn == 'Y')
                  {
                      accountList.Remove(pin);
                      cout &lt;&lt; "Account Removed Successfully" &lt;&lt; endl;
                  }
              }
              else
              {
                  cout &lt;&lt; "Invalid PIN or Account does not exist" &lt;&lt; endl;
              }
              choice = AccountManagerChooser();
              break;
          }
      case 4:
          //Print Customer Details
          printDetails();
          choice = 0;
          break;
      case 5:
          {
              //Change Customer Details
              int detailChange = 0;
  
              while (detailChange != -1)
              {
                  cout &lt;&lt; "1 Name" &lt;&lt; endl;
                  cout &lt;&lt; "2 Address" &lt;&lt; endl;
                  cout &lt;&lt; "3 Telephone No" &lt;&lt; endl;
                  cout &lt;&lt; "4 Gender" &lt;&lt; endl;
                  cout &lt;&lt; "5 Date of Birth" &lt;&lt; endl;
                  cout &lt;&lt; "Which piece of detail do you want to change? ";
                  cin &gt;&gt; detailChange;
                  string str;
                  getline(cin, str);
                  cout &lt;&lt; endl;
  
                  switch (detailChange)
                  {
                  case 1:
                      {
                          string newName;
                          cout &lt;&lt; "Enter new name: ";
                          getline(cin, newName);
                          setName(newName);
                          break;
                      }
  
                  case 2:
                      {
                          string newAddress;
                          cout &lt;&lt; "Enter new Address: ";
                          getline (cin,newAddress);
                          setAddress(newAddress);
                          break;
                      }
                  case 3:
                      {
                          string newTelephone;
                          cout &lt;&lt; "Enter new Telephone Number: ";
                          getline (cin,newTelephone);
                          setTelephoneNo(newTelephone);
                          break;
                      }
                  case 4:
                      {
                          string newGender;
                          cout &lt;&lt; "Enter Gender" &lt;&lt; endl;
                          getline (cin,newGender);
                          gender = newGender;
                          break;
                      }
                  case 5:
                      int newDay = 0;
                      int newMonth = 0;
                      int newYear = 0;
                      cout &lt;&lt; "Enter Day: ";
                      cin &gt;&gt; newDay;
                      cout &lt;&lt; "Enter Month: ";
                      cin &gt;&gt; newMonth;
                      cout &lt;&lt; "Enter Year: ";
                      cin &gt;&gt; newYear;
  
                      setDOB(newDay,newMonth,newYear);
                      break;
                  }
  
                  char yn = 0;
                  cout &lt;&lt; "Would you like to change anything else? (Y/N) ";
                  cin &gt;&gt; yn;
  
                  if (yn == 'Y' || yn == 'y')
                  {
                      detailChange = 0;
                      cout &lt;&lt; endl;
                  }
                  else
                  {
                      detailChange = -1;
                      cout &lt;&lt; endl;
                  }
              }
              choice = AccountManagerChooser();
              cout &lt;&lt; endl;
          }
          break;
      case 6:
          //Create a mortgage
          {
              time_t t = time(0);
              struct tm * now = localtime( &amp; t );
  
              CreateMortgageManager();
  
              choice = AccountManagerChooser();
              cout &lt;&lt; endl;
          }
          break;
  
      case 7:
          //Use a mortgage
          {
              string str;
              string pin;
              getline(cin,str);
              cout &lt;&lt; "Enter PIN: ";
              getline(cin,pin);
              UseMortgage(pin);
              choice = AccountManagerChooser();
              cout &lt;&lt; endl;
          }
          break;
  
      case 8:
          //Remove a mortgage
          {
              string str;
              string pin;
              getline(cin,str);
              cout &lt;&lt; "Enter PIN: ";
              getline(cin,pin);
              RemoveMortgage(pin);
              choice = AccountManagerChooser();
              cout &lt;&lt; endl;
          }
          break;
      case 9:
          break;
      default:
          cout &lt;&lt; endl;
          choice = AccountManagerChooser();
          break;
      }
  
      if (choice == 9)
      {
          cout &lt;&lt; "Thank you for your time!\n\n" &lt;&lt; endl;
      }
      else if (choice == 0)
      {
          choice = AccountManagerChooser();
      }
  }
  }
  
  int Customer::AccountManagerChooser()
  {
  cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
  cout &lt;&lt; "1: Create a new account" &lt;&lt; endl;
  cout &lt;&lt; "2: Use an account" &lt;&lt; endl;
  cout &lt;&lt; "3: Close an account" &lt;&lt; endl;
  cout &lt;&lt; "4: Show Personal Details" &lt;&lt; endl;
  cout &lt;&lt; "5: Update Personal Details" &lt;&lt; endl;
  cout &lt;&lt; "6: Create a Mortgage" &lt;&lt; endl;
  cout &lt;&lt; "7: Use a Mortgage" &lt;&lt; endl;
  cout &lt;&lt; "8: Remove a Mortgage" &lt;&lt; endl;
  cout &lt;&lt; "9: Exit" &lt;&lt; endl;
  cout &lt;&lt; "Enter Choice: ";
  int choice;
  cin &gt;&gt; choice;
  cout &lt;&lt; endl;
  return choice;
  }
  
  void Customer::CreateAccountManager()
  {
  int choice = 0;
  
  while (choice != -1)
  {
      time_t t = time(0);
      struct tm * now = localtime( &amp; t );
  
      cout &lt;&lt; "What account do you wish to create?" &lt;&lt; endl;
      cout &lt;&lt; "1: Corporate Savings" &lt;&lt; endl;
      cout &lt;&lt; "2: Current Account" &lt;&lt; endl;
      cout &lt;&lt; "3: Junior Account" &lt;&lt; endl;
      cout &lt;&lt; "4: Student Savings Account" &lt;&lt; endl;
      cout &lt;&lt; "5: Cancel" &lt;&lt; endl;
      cout &lt;&lt; "Enter Choice: ";
      cin &gt;&gt; choice;
      cout &lt;&lt; endl;
  
      switch (choice)
      {
      case 1:
          //Corporate Savings Account Made
          //Grabs age from current date
          if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &gt;= 16)
          {
              cout &lt;&lt; "Please enter how much you wish to deposit: ";
              double initDeposit;
              cin &gt;&gt; initDeposit;
  
              cout &lt;&lt; "Please enter a four digit number to set as the PIN: ";
              string PIN;
              cin &gt;&gt; PIN;
  
              cout &lt;&lt; "Please enter a 9 digit number to set as the account number: ";
              string AccountNo;
              cin &gt;&gt; AccountNo;
  
              AddAccount(PIN, new CorporateSavings(AccountNo, initDeposit, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
              cout &lt;&lt; "Corporate Savings Account Created" &lt;&lt; endl;
              choice = -1;
          }
          else if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &lt; 16)
          {
              cout &lt;&lt; "Corportate Savings Account is only available to customers over the age of 16" &lt;&lt; endl;
          }
          break;
      case 2:
          //Current Account Made
          //Grabs age from current date
          if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &gt;= 16)
          {
              cout &lt;&lt; "Please enter how much you wish to deposit: ";
              double initDeposit;
              cin &gt;&gt; initDeposit;
  
              cout &lt;&lt; "Please enter a four digit number to set as the PIN: ";
              string PIN;
              cin &gt;&gt; PIN;
  
              cout &lt;&lt; "Please enter a 9 digit number to set as the account number: ";
              string AccountNo;
              cin &gt;&gt; AccountNo;
  
              AddAccount(PIN, new CurrentAccount(AccountNo, initDeposit, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
              cout &lt;&lt; "Current Account Created" &lt;&lt; endl;
              choice = -1;
          }
          else if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &lt; 16)
          {
              cout &lt;&lt; "Current Account is only available to customers over the age of 16" &lt;&lt; endl;
          }
          break;
      case 3:
          //Junior Current Account Made
          //Grabs age from current date
          if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &lt;= 16)
          {
              cout &lt;&lt; "Please enter how much you wish to deposit: ";
              double initDeposit;
              cin &gt;&gt; initDeposit;
  
              cout &lt;&lt; "Please enter a four digit number to set as the PIN: ";
              string PIN;
              cin &gt;&gt; PIN;
  
              cout &lt;&lt; "Please enter a 9 digit number to set as the account number: ";
              string AccountNo;
              cin &gt;&gt; AccountNo;
  
              AddAccount(PIN, new JuniorCurrentAccount(AccountNo, initDeposit, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
              cout &lt;&lt; "Junior Account Created" &lt;&lt; endl;
              choice = -1;
          }
          else if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &gt; 16)
          {
              cout &lt;&lt; "Junior Account is only available to customers under the age of 16" &lt;&lt; endl;
          }
          else if (parent != 0)
          {
              cout &lt;&lt; "Customers applying for Junior Account must have an adult registered as a guardian" &lt;&lt; endl;
          }
  
          break;
      case 4:
          //Student Savings Account Made
          //Grabs age from current date
          if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &gt;= 18)
          {
              cout &lt;&lt; "Please enter how much you wish to deposit: ";
              double initDeposit;
              cin &gt;&gt; initDeposit;
  
              cout &lt;&lt; "Please enter a four digit number to set as the PIN: ";
              string PIN;
              cin &gt;&gt; PIN;
  
              cout &lt;&lt; "Please enter a 9 digit number to set as the account number: ";
              string AccountNo;
              cin &gt;&gt; AccountNo;
  
              AddAccount(PIN, new StudentSavings(AccountNo, initDeposit, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
              cout &lt;&lt; "Student Account Created" &lt;&lt; endl;
              choice = -1;
          }
          else if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &lt; 18)
          {
              cout &lt;&lt; "Student Account is only available to customers over the age of 18" &lt;&lt; endl;
          }
          break;
      case 5:
          choice = -1;
          break;
      default:
          cout &lt;&lt; "Not a valid choice" &lt;&lt; endl;
          break;
      }
  }
  }
  
  void Customer::AddAccount(string PIN, Account * a)
  {
  accountList.Add(PIN, a);
  }
  
  bool Customer::UseAccount(string PIN)
  {
  if (accountList.Get(PIN) != NULL)
  {
      int choice = 0;
  
      while (choice != -1)
      {
          cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
          cout &lt;&lt; "1: Deposit Cash" &lt;&lt; endl;
          cout &lt;&lt; "2: Deposit Cheque" &lt;&lt; endl;
          cout &lt;&lt; "3: Withdraw Cash" &lt;&lt; endl;
          cout &lt;&lt; "4: Show Balance" &lt;&lt; endl;
          cout &lt;&lt; "5: Exit" &lt;&lt; endl;
          cout &lt;&lt; "Enter Choice: ";
          cin &gt;&gt; choice;
          cout &lt;&lt; endl;
  
          switch (choice)
          {
          case 1:
              {
                  cout &lt;&lt; "Enter Deposit Amount: ";
                  double amount;
                  cin &gt;&gt; amount;
                  if (accountList.Get(PIN)-&gt;depositAmount(amount))
                  {
                      time_t t = time(0);
                      struct tm * now = localtime( &amp; t );
  
                      ofstream output;
                      output.open("Transactions.txt", ios::app);
                      output &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; " - Account Number: " &lt;&lt; accountList.Get(PIN)-&gt;AccountNumber() &lt;&lt; " - Deposit: £" &lt;&lt; 
                          amount &lt;&lt; " - Date: " &lt;&lt; now-&gt;tm_mday &lt;&lt; "/" &lt;&lt; now-&gt;tm_mon + 1 &lt;&lt; "/" &lt;&lt; now-&gt;tm_year + 1900 &lt;&lt; endl;
                      output.close();
                      cout &lt;&lt; "Amount Deposited Successfully" &lt;&lt; endl;
                  }
                  else
                      cout &lt;&lt; "Deposit Invalid" &lt;&lt; endl;
  
                  choice = 0;
              }
              break;
          case 2:
              {
                  time_t t = time(0);
                  struct tm * now = localtime( &amp; t );
  
                  ofstream output;
                  output.open("Transactions.txt", ios::app);
                  output &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; " - Account Number: " &lt;&lt; accountList.Get(PIN)-&gt;AccountNumber() &lt;&lt; " - Cheque Deposit: £" 
                      &lt;&lt; " - Date: " &lt;&lt; now-&gt;tm_mday &lt;&lt; "/" &lt;&lt; now-&gt;tm_mon + 1 &lt;&lt; "/" &lt;&lt; now-&gt;tm_year + 1900 &lt;&lt; endl;
                  output.close();
                  cout &lt;&lt; "Amount Deposited Successfully" &lt;&lt; endl;
              }
              break;
          case 3:
              {
                  cout &lt;&lt; "Enter Withdraw Amount: ";
                  double amount;
                  cin &gt;&gt; amount;
                  if (accountList.Get(PIN)-&gt;widthdrawAmount(amount))
                  {
                      time_t t = time(0);
                      struct tm * now = localtime( &amp; t );
  
                      ofstream output;
                      output.open("Transactions.txt", ios::app);
                      output &lt;&lt; "Name: " &lt;&lt; name &lt;&lt; " - Account Number: " &lt;&lt; accountList.Get(PIN)-&gt;AccountNumber() &lt;&lt; " - Withdrawal: £" &lt;&lt; 
                          amount &lt;&lt; " - Date: " &lt;&lt; now-&gt;tm_mday &lt;&lt; "/" &lt;&lt; now-&gt;tm_mon + 1 &lt;&lt; "/" &lt;&lt; now-&gt;tm_year + 1900 &lt;&lt; endl;
                      output.close();
                      cout &lt;&lt; "Amount Withdrawn Successfully" &lt;&lt; endl;
                  }
                  else
                      cout &lt;&lt; "Withdraw Invalid" &lt;&lt; endl;
  
                  choice = 0;
              }
              break;
          case 4:
              {
                  time_t t = time(0);
                  struct tm * now = localtime( &amp; t );
  
                  accountList.Get(PIN)-&gt;addInterest(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900);
  
                  cout &lt;&lt; "Balance: £" &lt;&lt; accountList.Get(PIN)-&gt;Amount() &lt;&lt; endl;
  
                  choice = 0;
              }
              break;
          case 5:
              choice = -1;
              break;
          default:
              choice = 0;
              break;
          }
  
          char yn;
          cout &lt;&lt; "Do you want to use another service? (Y/N) ";
          cin &gt;&gt; yn;
  
          if ((yn == 'y' || yn == 'Y') &amp;&amp; choice != -1)
          {
              choice = 0;
          }
          else
          {
              choice = -1;
          }
      }
  
      return true;
  }
  return false;
  }
  
  void Customer::RemoveAccount(string PIN)
  {
  char choice;
  
  cout &lt;&lt; "Are you sure? (Y/N): ";
  cin &gt;&gt; choice;
  cout &lt;&lt; endl;
  while (choice != 'y' || choice != 'Y' || choice != 'n' || choice != 'N')
  {
      cout &lt;&lt; "Are you sure? (Y/N): ";
      cin &gt;&gt; choice;
      cout &lt;&lt; endl;
  }
  
  accountList.Remove(PIN);
  }
  
  void Customer::CreateMortgageManager()
  {
  //Grabs age from current date
  time_t t = time(0);
  struct tm * now = localtime( &amp; t );
  
  if (getAge(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900) &gt;= 18)
  {
      int choice = 0;
  
      cout &lt;&lt; "Please enter the value of your House/Apartment: ";
      double value;
      cin &gt;&gt; value;
  
      cout &lt;&lt; "Please enter the length of the payment (years) : 1, 2 or 4" &lt;&lt; endl;
      int length;
      cin &gt;&gt; length;
  
      cout &lt;&lt; "Please enter the PIN number of the bank account you wish to pay installments to: ";
      string accountPIN;
      cin &gt;&gt; accountPIN;
  
      if (accountList.Get(accountPIN) == NULL)
      {
          while (accountList.Get(accountPIN) == NULL)
          {
              cout &lt;&lt; "Account PIN not valid or Account doesn't exist\n" &lt;&lt; endl;         
              cout &lt;&lt; "Please enter the PIN number of the bank account you wish to pay installments to: ";
              cin &gt;&gt; accountPIN;
          }
      }
  
      while (choice != -1)
      {
          cout &lt;&lt; "What kind of Mortgage do you wish to create?" &lt;&lt; endl;
          cout &lt;&lt; "1: Fixed Rate Mortgage : " &lt;&lt; ((value*0.8)+50)/(length * 12) &lt;&lt; " per month with 5% Interest" &lt;&lt; endl;
          cout &lt;&lt; "2: Discount Mortgage   : " &lt;&lt; ((value*0.8)+80)/(length * 12) &lt;&lt; " per month with 3.8% Interest (5% Interest after 2 years)" &lt;&lt; endl;
          cout &lt;&lt; "3: Buy to Let Mortgage : "  &lt;&lt; ((value*0.8)+120)/(length * 12) &lt;&lt; " per month with 7.35% Interest" &lt;&lt; endl;
          cout &lt;&lt; "4: Cancel" &lt;&lt; endl;
          cout &lt;&lt; "Enter Choice: ";
          cin &gt;&gt; choice;
          cout &lt;&lt; endl;
  
          string str;
          getline(cin,str);
  
          switch (choice)
          {
          case 1:
              {
                  cout &lt;&lt; "Please enter a PIN, so you can access your mortgage securely: ";
                  string PIN;
                  getline(cin,PIN);
  
                  AddMortgage(PIN, new FixedRateMortgage(value, 50.0, accountList.Get(accountPIN), length, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
                  cout &lt;&lt; "Fixed Rate Mortgage Created" &lt;&lt; endl;
                  choice = -1;
              }
              break;
          case 2:
              {
                  cout &lt;&lt; "Please enter a PIN, so you can access your mortgage securely: ";
                  string PIN;
                  getline(cin,PIN);
  
                  AddMortgage(PIN, new DiscountMortgage(value, 45.0, accountList.Get(accountPIN), length, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
                  cout &lt;&lt; "Discount Mortgage Created" &lt;&lt; endl;
                  choice = -1;
              }
              break;
          case 3:
              {
                  cout &lt;&lt; "Please enter a PIN, so you can access your mortgage securely: ";
                  string PIN;
                  getline(cin,PIN);
  
                  AddMortgage(PIN, new BuyToLetMortgage(value, 70.0, accountList.Get(accountPIN), length, now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900));
                  cout &lt;&lt; "Buy To Let Mortgage Created" &lt;&lt; endl;
                  choice = -1;
              }
              break;
          case 4:
              choice = -1;
              break;
          default:
              cout &lt;&lt; "Not a valid choice" &lt;&lt; endl;
              break;
          }
      }
  }
  else
  {
      cout &lt;&lt; "Mortgages are only available to customers over the age of 18" &lt;&lt; endl;
  }
  }
  
  void Customer::AddMortgage(string PIN, Mortgage * m)
  {
  mortgageList.Add(PIN, m);
  }
  
  void Customer::UseMortgage(string PIN)
  {
  if (mortgageList.Get(PIN) != NULL)
  {
      int choice = 0;
  
      while (choice != -1)
      {
          cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
          cout &lt;&lt; "1: Pay Installment" &lt;&lt; endl;
          cout &lt;&lt; "2: Print Payment List" &lt;&lt; endl;
          cout &lt;&lt; "3: Exit" &lt;&lt; endl;
          cout &lt;&lt; "Enter Choice: ";
          cin &gt;&gt; choice;
          cout &lt;&lt; endl;
  
          switch (choice)
          {
          case 1:
              {
                  time_t t = time(0);
                  struct tm * now = localtime( &amp; t );
  
                  mortgageList.Get(PIN)-&gt;PayInstallment(now-&gt;tm_mday, now-&gt;tm_mon + 1, now-&gt;tm_year + 1900);
  
                  choice = 0;
              }
              break;
          case 2:
              {
                  mortgageList.Get(PIN)-&gt;PrintPaymentLog();
              }
              break;
          case 3:
              choice = -1;
              break;
          default:
              choice = 0;
              break;
          }
  
          char yn;
          cout &lt;&lt; "Do you want to use another service? (Y/N) ";
          cin &gt;&gt; yn;
  
          if ((yn == 'y' || yn == 'Y') &amp;&amp; choice != -1)
          {
              choice = 0;
          }
          else
          {
              choice = -1;
          }
      }
  }
  }
  
  void Customer::RemoveMortgage(string PIN)
  {
  char choice;
  
  cout &lt;&lt; "Are you sure? (Y/N): ";
  cin &gt;&gt; choice;
  cout &lt;&lt; endl;
  while (choice != 'y' || choice != 'Y' || choice != 'n' || choice != 'N')
  {
      cout &lt;&lt; "Are you sure? (Y/N): ";
      cin &gt;&gt; choice;
      cout &lt;&lt; endl;
  }
  
  mortgageList.Remove(PIN);
  }
  </code></pre>