Program Crashes On Exit
Hi, I'm having some issues with my program. It crashes on exit. Could this be a problem with the destructor?
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
|
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
template<class Type>
class CircularList
{
public:
template<class Type2>
friend ostream& operator<<(ostream&, const CircularList<Type>&);
void AdvanceN(int size);
int length();
void DeleteThis();
void Eliminated();
CircularList(int size);
~CircularList();
int count;
int *jose; //pointer to array
int remain;
int idx; //tracks position
int maxsize;
vector<int> myVector;
};
//Construtor
template<class Type>
CircularList<Type>::CircularList(int size)
{
idx=1;
remain = size; //number of people remaining
count = 0;
int num = 0;
jose = new int[size];
maxsize = size;
for(int i = 1; i <= size; i++){ //sets each element of array to value of true
jose[i] = i;
count++;
}
}
template <class Type>
void CircularList<Type>::AdvanceN(int adv)
{
for(int i=0; i<adv; ++i){
cout<<"idx is: "<<idx<<endl;
idx++;
while (jose[idx]==-1){
idx++;
cout<<"skip"<<endl;
}
if(idx > maxsize){ //make array circular.
idx = 1;
if (jose[idx]==-1){
while (jose[idx] == -1){
idx++;
}
} //if at end, loop to beginning
}
}
cout<<"You are now here: "<<jose[idx]<<endl<<endl;
}
template <class Type>
void CircularList<Type>::DeleteThis()
{
//int j=0;
cout << idx <<" Not Here"<< endl;
//myVector.push_back(idx);
//cout<<"my vector: "<<myVector[j]<<endl;
//j++;
jose[idx] = -1; //removes the person from group by setting element value to 0
count--;
//cout<<"remain is: "<<remain<<endl;
idx--; //fall back to previous position
if(idx < 1 && idx!= -1){
idx = maxsize;
//cout<<"maxsize is: "<<maxsize<<endl;
if(jose[maxsize]==-1){
while(jose[idx]==-1)
{
cout<<"idx is: "<<idx<<endl;
idx--;
}
}
}
}
template<class Type>
void CircularList<Type>::Eliminated()
{
cout<<"Eliminated in this order:"<<endl;
for(int j=0; j<myVector.size(); j++)
{
cout<<myVector[j]<<endl;
}
}
template<class Type>
int CircularList<Type>::length()
{
return count;
}
template<class Type>
ostream& operator<<(ostream& osObject, const CircularList<Type>& list)
{
for(int i=1;i<=list.maxsize;i++)
{
if(list.jose[i]!=-1){
osObject<<list.jose[i]<<" ";
}
}
return osObject;
}
template<class Type>
CircularList<Type>::~CircularList()//destructor
{
delete[] jose; //clear the memory used by the array
jose = NULL;
cout << "CircularList::~CircularList()" << endl;
}
int main()
{
int adv = 0;
int num = 0;
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);
cout<<mylist;
do{
cout<<endl<<endl<<"Enter a starting person: "<<endl;
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
//add do while check here
do{
cout<<endl<<"Enter Mth number to advance people by: ";
cin>>adv;
if (adv>0){
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;
}
valid = true;
}
else{
cerr<<"Invalid entry! Try again."<<endl<<endl;
}
}while(!valid);
valid = false; //reset valid
mylist.Eliminated();
cout<<endl;
cout<<mylist<<"is the winner!"<<endl<<endl;
cout<<"Game over";
cin.ignore();
cin.get();
return 0;
}
|
What makes you think the program crashes?
Array indices go from 0 to n-1
Also, ¿is it so hard to indent code?
Peter87, I was getting a Window's "program has stopped working" error message.
ne555, thank you, it was because I went out of bounds with my array.
Topic archived. No new replies allowed.