guys I've been trying to write this lab for my comp sci class for three days now. i've got to the point where it compiles but when I try to run it a table pops up that's says there's an error somewhere in the program that causes it to abort. the program is a simple one, it uses pointer based lists. Basically I'm supposed to write a code for a parking lot with two lanes. When a car arrives I have to find a spot for it on either of the two lanes. And if a car departs I remove it from whichever lane it is located in. I'm in desperate need of help can someone please give it a look :?:

#include <cstddef>     // for NULL
#include <new>         // for bad_alloc
#include "ListP.h" // header file

using namespace std;

// definitions of methods follow:
//   . . .
List::List():head(NULL),size(0) {

List::List(const List& aList)
: size(aList.size)
	if (aList.head == NULL)
		head = NULL;  // original list is empty
	{  // copy first node
		head = new ListNode;
		head->item = aList.head->item;
		// copy rest of list
		ListNode *newPtr = head;  // new list pointer
		// newPtr points to last node in new list
		// origPtr points to nodes in original list
		for (ListNode *origPtr = aList.head->next;origPtr != NULL;origPtr = origPtr->next)
		{  newPtr->next = new ListNode;
		newPtr = newPtr->next;
		newPtr->item = origPtr->item;
		}  // end for
		newPtr->next = NULL;	//After the for statements is evaluated as invalid because origPtr->next=NULL; we assigne the last node to point to NULL!
	}  // end if
}  // end copy constructor

	while (!isEmpty())
}  // end destructor

bool List::isEmpty() const
	return size == 0;
}  // end isEmpty

int List::getLength() const
	return size;
}  // end getLength

List::ListNode *List::find(int index) const
	if ( (index < 1) || (index > getLength()) )	//getLength()=size;
		return NULL;
	else  // count from the beginning of the list.
	{  ListNode *cur = head;
	for (int skip = 1; skip < index; ++skip)
		cur = cur->next;
	return cur;
	}  // end if
}  // end find

ListItemType List::retrieve(int index) const
	ListItemType dataItem;
	if ( (index < 1) || (index > getLength()) )
		throw (ListIndexOutOfRangeException)("ListIndexOutOfRangeException: retrieve index out of range");
	{  // get pointer to node, then data in node
		ListNode *cur = find(index);
		dataItem = cur->item;
	return dataItem;// end if
}  // end retrieve

void List::insert(int index, const ListItemType& newItem)
throw(ListIndexOutOfRangeException, ListException)
	int newLength = getLength() + 1;
	if ( (index < 1) || (index > 10) )
		throw (ListIndexOutOfRangeException)("ListIndexOutOfRangeException: insert index out of range");
	{  // try to create new node and place newItem in it
			ListNode *newPtr = new ListNode;
			size = newLength;
			newPtr->item = newItem;
			// attach new node to list
			if (index == 1)
			{  // insert new node at beginning of list
				newPtr->next = head;
				head = newPtr;
			{  ListNode *prev = find(index-1);
            // insert new node after node
            // to which prev points
            newPtr->next = prev->next;
			prev->next = newPtr;
			}  // end if
		}  // end try
		catch (bad_alloc e)
			throw (ListException)("ListException: memory allocation failed on insert");
		}  // end catch
	}  // end if
}  // end insert

void List::remove(int index) throw(ListIndexOutOfRangeException)
	ListNode *cur;
	if ( (index < 1) || (index > 10) )
		throw (ListIndexOutOfRangeException)(
		"ListIndexOutOfRangeException: remove index out of range");
	{  --size;
	if (index == 1)
	{  // delete the first node from the list
		cur = head;  // save pointer to node
		head = head->next;
	{  ListNode *prev = find(index - 1);
	// delete the node after the node to which prev points
	cur = prev->next;  // save pointer to node
	prev->next = cur->next;
	}  // end if
	// return node to system
	cur->next = NULL;
	delete cur;
	cur = NULL;
	}  // end if
}  // end remove

int main ()
	List garage1,garage2;
	ListItemType licensePlate;

	char car;

	cout<<"Please enter A/a if you want to park your car in our garage"<<endl;
	cout<<"Or enter B/b if you want to find and take your car from our garage"<<endl;
	if (car=='A'||car=='a'&&garage1.getLength()<10)
				cout<<"Please enter the license plate of the car you wish to park in the first garage"<<endl;

				 for(int i=0;i<garage1.getLength()+1;i++)
				 cout<<garage1.retrieve(i)<<"   ";


	else if(car=='A'||car=='a'&&garage1.getLength()==10&&garage2.getLength()<10)
		cout<<"First Garage is full!"<<endl;

			cout<<"Please enter the license plate of the car you wish to park in the second garage"<<endl;

			for(int i=0;i<garage2.getLength()+1;i++)
			cout<<garage2.retrieve(garage2.getLength())<<"    ";
	else if(car=='A'||car=='a'&&garage1.getLength()==10&&garage2.getLength()==10)
		cout<<"Sorry both our garages are full"<<endl;

	else if(car=='B'||car=='b')

 {cout<<"Please enter the license plate of your car"<<endl;
		for(int i=0;i<garage1.getLength()+1;i++)
					{cout<<"Car found in garage1. Removing it!"<<endl;
				else if (garage2.retrieve(i)==licensePlate)
			cout<<"Car found in garage 2. Removing it!"<<endl;

		cout<<"We're sorry! We couldn't find your car!"<<endl;

			for(int i=0;i<garage1.getLength()+1;i++)
				cout<<"Garage1:  "<<garage1.retrieve(i)<<"    ";

			for(int i=0;i<garage2.getLength()+1;i++)
				cout<<"Garage2:  "<<garage2.retrieve(i)<<"    ";

	return 0;
6 Years
Discussion Span
Last Post by daviddoria

I would very much suggest you hard code values into the program as you debug. Once you're convinced it's working, then you can add in the user input. This will also help us help you - it would be very complicated to explain exactly the sequence of input values you used - whereas if you could post compilable code that demonstrates the problem that would be much easier. However, even if you post compilable and run-albe code, it is unlikely that anyone is going to debug your 200 lines. You really should attempt to narrow the problem down so we can look at a specific function or something like that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.