Implement the template class SortedList. Write a simple driver that reads values from file float.txt, inserts them into the list, prints the length of the final list, and prints the items. Add code to your driver to test the remaining member functions. Delete 3.3, 10.0, and 200.0 and print the list to be sure they are gone.

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 line 157 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?

My float text file looks like this:
5.5
6.2
7.1
8.0
9.0
10.0
1.0
2.0
3.3
4.4


CODE:

#include <iostream>
#include <fstream>
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 const
{
    NodeType *location = listData;

    while (listData != NULL) {
	if (location->info = item) {
	    return true;
	} else {
	    location = location->next;
	}
    }
    return false;
}

int SortedType::GetLength() const 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 < item) {
	    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();


}

It runs fine for me. I just had to change two things where you had 'const' twice:

int SortedType::GetLength() const const

to

int SortedType::GetLength() const

Can anyone else produce sharifyboy7's problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.