Program has stopped working error at exit

My program runs fine until it exits, where it displays a Window's error that the program has stopped working. Could someone lead me in the right direction on how to deal with this? Thanks.

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

//definition of the node
template<class Type>
struct node
{  
    int data;  
    node<Type> *link; 
};  

template<class Type>
class CircularList
{
      template<class Type2>
      friend ostream& operator<<(ostream&, const CircularList<Type>&);
      //overload the stream insertion operator.
      
      public:
      const CircularList<Type>& operator=(const CircularList<Type>&);
      //overload the assignment operator
      
      void AdvanceN(const Type& newItem);
      
      void DeleteThis();
      
      void Eliminated();
      
      void initializeList();
      //function to initialize the list to an empty state.
      //postcondition: first = null; last = null; count = 0;
      
      bool isEmptyList();
      //function to determin whether the list is empty.
      //postcondition: returns true if the list is empty; otherwise, returns false.
      
      int length();
      //function to return the number of nodes in the list.
      //postcondition: the value of count is returned
      
      void destroyList();
      //function to delete all the nodes from the list.
      //postcondition: first = null; last = null; count = 0;
      
      Type front();
      //function to return the first element of the list.
      //precondition: the list must exist and must not be empty.
      //postcondition: if the list is empty, then the program terminates
      //otherwise, the first element of the list is returned.
      
      Type back();
      //function to return the last element of the list.
      //precondition: the list must exist and must not be empty.
      //post condition: if the list is empty, then the program terminates.
      //otherwise, the last element of the list is returned.
  
      
      void insertLast(const Type& newItem);
      
      CircularList(const Type& newItem);
      //default constructor
      
      CircularList(const CircularList<Type>& otherList);
      //copy constructor
      
      ~CircularList();
      //destructor
      
      int count;
      node<Type> *first, *last, *newNode;
      node<Type> *current;
      
      vector<int> myVector;

      private:  
      
      void copyList(const CircularList<Type>& otherList);
      //function to make a copy of otherList
};


template <class Type>
void CircularList<Type>::AdvanceN(const Type& newItem)
{   
        if(first==NULL)
        return; 
        
        node<Type> *temp;
        if(current==NULL){
           current = first;
        }
        else{
           temp=current; 
        }
        for(int i=1;i<=newItem;++i){
           current=current->link;                 
        }
        cout<<"You are here: "<<current->data<<endl; 

}

template <class Type>
void CircularList<Type>::DeleteThis()
{
        if(first==NULL)
        return; 
        
        
        node<Type> *temp;
        if(current==NULL){
           current = first;
        }
        else{
           temp=current; 
        }
        
while(current->link!=temp){
   current=current->link;
}      
       current->link=temp->link;
       if(temp==first){
            first = temp->link;
       }
       if(temp==last){
                last = current;      
       }
   myVector.push_back(temp->data);
       //cout<<endl<<temp->data<<endl;
   delete temp;
   count--;
}

template<class Type>
void CircularList<Type>::Eliminated()
{
     int j = 0;
     cout<<"Eliminated in this order:"<<endl;
     for(int j; j<myVector.size(); j++)
     {
        cout<<myVector[j]<<endl;
     }
}


template<class Type>
bool CircularList<Type>::isEmptyList()
{
     return(first==NULL);
}

//Default Constructor
template<class Type>
CircularList<Type>::CircularList(const Type& newItem)
{
first = NULL;
last = NULL;
count = 0;
int i = 1;

while (i<=newItem)       
    {                                    
        insertLast(i);  
        i++;                                 
    } 
cout<<endl<<"There are "<<length()<<" people standing in a circle:\n";  
}


template<class Type>
void CircularList<Type>::destroyList()
{
     node<Type> *temp;
     
     while(first!= NULL)
     {
          temp = first;
          first = first->link;
          delete temp;
     }
              
          last = NULL;
          
          count = 0;
}
                   
     template<class Type>
     void CircularList<Type>::initializeList()
     {
        destroyList();
     }
     
     //Overloads << operator
     template<class Type>
     ostream& operator<<(ostream& osObject, const CircularList<Type>& list)
      {
            node<Type> *current; //pointer to traverse the list
            current = list.first;// points to the first node
                             
            for(int i=1;i<=list.count;i++) 
            {
                 osObject<<current->data<<" ";
                 current = current->link;
            }
       return osObject;
       }
                                           
      template<class Type>
      int CircularList<Type>::length()
      {
          return count;
      }
      
      //returns info of the first node    
      template<class Type>
      Type CircularList<Type>::front()
      {
           assert(last != NULL);
           return first->data;
      }
               
      //returns info of the last node    
      template<class Type>
      Type CircularList<Type>::back()
      {
           assert(last != NULL);
           return last->data;
      }         

      
      template<class Type>
      void CircularList<Type>::insertLast(const Type& newItem)
      {
           node<Type> *newNode;
           
           newNode = new node<Type>;
           
           assert(newNode != NULL);
           
           newNode->data = newItem;
           
           newNode->link = NULL;
           
           if(first == NULL)
           {
                 first = newNode;
                 last = newNode;
                 newNode->link=first;    //point back to first node to be circular
                 count++;
           }
           else // the list is not empty, insert newnode after last
           {
                 last->link = newNode;
                 last = newNode;
                 count++;
                 newNode->link=first;     //point back to first node to be circular
           }
      }
                 
      

      
More code...
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
      template<class Type>
      void CircularList<Type>::copyList(const CircularList<Type>& otherList)
      {
           node<Type> *newNode;
           node<Type> *current;
           
           if(first != NULL) //if the list is not empty, make it empty
                destroyList();
                
           if(otherList.first == NULL)     //other list is empty
           {
                first = NULL;
                last = NULL;

                count = 0;
           }
           else
           {
               current = otherList.first;
               
               count = otherList.count;
               
               //copy the first node
               first = new node<Type>;//create the node
               
               assert(first != NULL);
               
               first->data = current->info;
               first->link = NULL;
               
               last = first;
               
               current = current->link;
               
               //copy the remaining list
               while(current != NULL)
               {
                    newNode = new node<Type>;  //create a node
                    
                    assert(newNode!= NULL);
                    
                    newNode->data = current->info;
                    newNode->link = NULL;
                    
                    last->link = newNode;
                    
                    last = newNode;
                    current = current->link;
                    
               }//end while
           }//end else
      }//end copyList
               
      template<class Type>
      CircularList<Type>::~CircularList()//destructor
      {
           destroyList();
      }

template<class Type>
      CircularList<Type>::CircularList(const CircularList<Type>& otherList)
      {
           first = NULL;
           copyList(otherList);

      }
      
      template<class Type>
      const CircularList<Type>& CircularList<Type>::operator=(const CircularList<Type>& otherList)
      {
           if(this != &otherList)//avoid self-copy
           copyList(otherList);
           
           return *this;
      }


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
#include "CircularListLINKED.cpp"

int main()
{
int num,adv;
char choice;
bool valid = false;   

do{
cout<<"Enter size of circle: ";
cin>>num;
if(num>1)
{         
valid = true;
}
else{
     cerr<<"Enter a number greater than 1."<<endl<<endl;
     }
}while(!valid);
valid = false; //reset valid    

CircularList<int> mylist(num);

do{
cout<<endl<<endl<<"Enter a starting person: ";
cin>>adv;
adv=adv-1;

if (adv<num&&adv>=0){  
mylist.AdvanceN(adv);
valid = true;
}
else{
     cerr<<"Invalid entry! Try again."<<endl<<endl;
     }
}while(!valid);
valid = false; //reset valid

cout<<endl<<"Enter Mth number to advance people by: ";
cin>>adv;

while(mylist.length()>1){
                              
       mylist.AdvanceN(adv); 
       do{
           cout<<"Do you wish to delete this node? y = yes, n = no ";
           cin>>choice;
             if (choice == 'y')
              {
              mylist.DeleteThis();
              valid = true;
              }
              else if(choice == 'n')
              {
              valid = true;
              }
              else{
              cerr<<endl<<"That is an incorrect choice. Try again."<<endl;
              }
          }while(!valid);
       //cout<<mylist<<endl<<endl;              
}


mylist.Eliminated();
cout<<endl;
cout<<mylist<<"is the winner!"<<endl<<endl;
cout<<"Game over";


cin.ignore();
cin.get();
return 0;
}
Where is main() ?
It's the last block of code. Sorry for the break in between the code of the first file. cplusplus.com yelled at me for the length I was trying to post.
The most likely thing is something is going wrong in the destructor. You probably are trying to delete something that doesn't exist. (Perhaps deleting something twice).

Your are supposed to include .h files, not .cpps
Last edited on
You guys rock! Thanks for the help. Turns out I was deleting temp twice, once in the DeleteThis() method and a second time in the destroyList() method that gets called in the destructor. I removed the while loop in destroyList(). I've also changed the header file from a .cpp file to a .h file.
Topic archived. No new replies allowed.