After I implement the following function:

void List::insert(int index, const ListItemType& newItem)
   throw(ListIndexOutOfRangeException, ListException)
{
   int newLength = getLength() + 1;


   if ((index < 1) || (index > newLength))
      throw ListIndexOutOfRangeException(
     "ListIndexOutOfRangeException: insert index out of range");
   else
   { 
      try
      {
     ListNode *newPtr = new ListNode;
     size = newLength;

     newPtr->name = "Adam";
     newPtr->ic = "881122045599";
     newPtr->city = "Melaka";
     newPtr->pc = "75350";
     newPtr->country = "Malaysia";
     newPtr->phone = "017-1234567";
     newPtr->email = "abc123xyz@hotmail.com";

     cout<<"Name: "<<endl;
     cin>>newPtr->name;
     cout<<"IC: "<<endl;
     cin>>newPtr->ic;
     cout<<"City: "<<endl;
     cin>>newPtr->city;
     cout<<"PostalCode: "<<endl;
     cin>>newPtr->pc;
     cout<<"Country: "<<endl;
     cin>>newPtr->country;
     cout<<"Phone: "<<endl;
     cin>>newPtr->phone;
     cout<<"Email: "<<endl;
     cin>>newPtr->email;

     newPtr->next = NULL;

     if(index == 1)
     {  
        newPtr->next = head;
        head = newPtr;
     }
     else
     {  ListNode *prev = find(index-1);
        newPtr->next = prev->next;
        prev->next = newPtr;
     }  
  }  
  catch(bad_alloc e)
  {
  throw ListException("ListException: memory allocation failed on insert");
  } 
   } 
}  

The program come out with this when running:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
And this is how I call in the main function:
alist.insert(index,newItem);

I'd appreciate it if you could help :)

what compiler are you using? Try debugging it (single-step through the program until you find the line that produces the error). Many good IDEs will have debuggers that let you do that. Check for uninitialized pointers.

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.