Problems with one compiler error for Sorted List program
Nov 24, 2010 at 1:35am UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#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();
}
Nov 24, 2010 at 11:39am UTC
Hi,
after trying your code for a while I managed to find the error
it is the second last line
SortedType a2(intList);
you do not have a constructor which takes a parameter
and when exiting the code is trying to delete an empty list
removing this line makes the code run as expected
Hope this was Helpful
Shredded
Topic archived. No new replies allowed.