david
2010-11-23 17:21:21 UTC
My program was able to run with the correct output but a small white screen keeps poping up saying "Debug Assertion Failed". When I clicked RETRY, my compiler then tells me that "cis200prog3part2.exe has triggered a breakpoint". The breakpoint is pointing toward the last line in my SortedType destructor function which is " delete tempPtr; ".Basically there is a problem with my SortedType destructor function which I am not sure how to fix. Can anyone help me fix this? Thanks.
CODE:
#include
#include
using namespace std;
typedef float ItemType;
struct NodeType;
typedef NodeType * NodePtr;
class SortedType
{
public:
SortedType(); // Class constructor
~SortedType(); // Class destructor
bool IsThere(ItemType item) const;
int GetLength() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
void Print();
int Length(){return length;};
private:
NodeType * listData;
int length;
NodeType * currentPos;
};
struct NodeType
{
ItemType info;
NodeType* next;
};
SortedType::SortedType() // Class constructor
{
length = 0;
listData = NULL;
}
bool SortedType::IsThere(ItemType item) const
{
NodeType *location = listData;
while(listData!=NULL)
{
if(location->info = item)
{
return true;
}
else
{
location = location->next;
}
}
return false;
}
int SortedType::GetLength() const
{
return length;
}
void SortedType::RetrieveItem(ItemType& item, bool& found)
{
bool moreToSearch;
NodeType* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
if (location->info > item)
{
location = location->next;
moreToSearch = (location != NULL);
}
else if (location->info < item)
{
found = true;
item = location->info;
}
else
moreToSearch = false;
}
}
void SortedType::InsertItem(ItemType item)
{
NodeType* newNode;
NodeType* predLoc;
NodeType* location;
bool moreToSearch;
location = listData;
predLoc = NULL;
moreToSearch = false;
while (location != NULL && !moreToSearch)
{
if (location->info
predLoc = location;
location = location->next;
}
else
moreToSearch = true;
}
newNode = new NodeType;
newNode->info = item;
if (predLoc == NULL)
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}
void SortedType::DeleteItem(ItemType item)
{
NodeType* tempLocation ;
NodeType* location = listData ;
if ( item == location->info)
{ tempLocation = location;
listData= listData->next ;
}
else {
while ( location->next->info != item )
location = location->next ;
tempLocation = location->next ;
location->next = location->next->next ;
}
delete tempLocation ;
length--;
}
void SortedType::ResetList()
{
currentPos = NULL;
}
void SortedType::Print()
{
NodeType * temp = listData;
while(temp != NULL)
{
cout << temp->info << endl;
temp = temp->next;
}
}
SortedType::~SortedType() // Class destructor
{
NodeType* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
int main()
{
float num;
SortedType intList;
ifstream inFile;
inFile.open("float1.txt");
while(inFile >> num)
{
intList.InsertItem(num);
}
int a = intList.GetLength();
cout << "The List contains " << a << " items:" << endl;
intList.Print();
intList.ResetList();
intList.DeleteItem(3.3);
intList.DeleteItem(10.0);
intList.DeleteItem(2.0);
SortedType a2(intList);
intList.Print();
}