I have been working on this code as part of an assignment for a class. With the code shown below when I try to delete the allocated memory for the two dimensional arrays the program will hang for 10-15 seconds. I realize there are sections of this code missing and it isn't the prettiest thing in the world. I would like to stick with the pointer arrays rather than using vectors for this because of my class as well. I can post the code in it's entirety if that would be more beneficial. If anyone can tell me what I've done wrong here I'd greatly appreciate. Thanks in advance for your help.
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
char **pName;
int *year;
char **bName;
char **cardNum;
char **teamName;
float *price;
char **note;
void menu();
int numOfCards(char fileName[]);
void allocArrMem(char fileName[]);
void deallocArrMem();
void getDBInfo(char fileName[]);
void menu()
{
int menuSelection;
char fileName[256];
cout << "Please enter the database filename:";
cin >> fileName;
cout << endl;
allocArrMem(fileName);
menuSelection = menuSelect();
getDBInfo(fileName);
deallocArrMem();
}
int numOfCards(char fileName[])
{
fstream cardDB;
cardDB.open(fileName);
int count=0;
char temp[100];
if(cardDB)
{
while(!cardDB.eof())
{
cardDB >> ws;
cardDB.getline(temp, 100);
count++;
cardDB >> ws;
}
}
cardDB.close();
return(count);
}
void allocArrMem(char fileName[])
{
int cardCount;
int i;
cardCount = numOfCards(fileName);
if (cardCount)
{
pName=new char*[26];
for(i=0; i<26; ++i)
pName[i]=new char[cardCount];
year=new int[cardCount];
price=new float[cardCount];
bName=new char*[16];
for(i=0; i<16; ++i)
bName[i]=new char[cardCount];
cardNum=new char*[6];
for(i=0; i<6; ++i)
cardNum[i]=new char[cardCount];
teamName=new char*[21];
for(i=0; i<21; ++i)
teamName[i]=new char[cardCount];
note=new char*[11];
for(i=0; i<11; ++i)
note[i]=new char[cardCount];
}
}
void deallocArrMem()
{
if(pName && bName && cardNum && teamName && note && year && price)
{
int t;
for(t = 0; t < 26; ++t)
delete []pName[t];
delete []pName;
delete []year;
delete []price;
for(t = 0; t < 16; ++t)
delete []bName[t];
delete []bName;
for(t = 0; t < 6; ++t)
delete []cardNum[t];
delete []cardNum;
for(t = 0; t < 21; ++t)
delete []teamName[t];
delete []teamName;
for(t = 0; t < 11; ++t)
delete []note[t];
delete []note;
}
}
void getDBInfo(char fileName[])
{
int iter;
fstream DBFile;
DBFile.open(fileName);
iter=0;
if (DBFile)
{
while(DBFile.good())
{
cout <<iter;
DBFile.get(pName[iter],26);
DBFile >> year[iter];
DBFile.get(bName[iter],16);
DBFile.get(cardNum[iter],6);
DBFile.get(teamName[iter],21);
DBFile >> price[iter];
DBFile.get(note[iter],11);
DBFile >> ws;
}
}
DBFile.close();
}