Need help with createCDs function
/*
*/
#include "CDs.h"
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
//
void resize (CDs* cds)
{
int cd_nums = cds -> num_cds;
int cd_max = cds -> max_cds;
CD** cd = cds -> cd_array;
for (int i = 0; i < cd_nums; i++)
{
cd[i] = cds->cd_array[i];
}
delete cd;
cd_max * 2;
}
//
CDs* createCDs(const char* filename,fstream &file)
{
const char* _file;
int max = 1;
int min = 0;
CDs* _cds = new CDs;
_cds -> num_cds = min;
_cds -> max_cds = max;
CD** cdPtrArray = new CD*[_cds->max_cds];
_cds -> cdPtrArray;
file.open(filename);
while (!file.eof())
{
int year, rating, num_tracks;
string* title = new string[1];
string* artist = new string[1];
string* length = new string[1];
file >> artist[0];
file >> title[0];
file >> year;
file >> rating;
file >> num_tracks;
CD* c = createCD(artist, title, year, rating, num_tracks);
cdPtrArray[num_cds] = c;
if (num_count == max_count)
{
resize (cds);
}
for (int i = 0; i < num_tracks; i++)
{
file >> length[0];
file >> title[0];
addSong(cd, title, length);
}
num_cds++;
}
return _cds;
}
//
void destroyCDs (CDs* cds)
{
int numCDs = cds -> num_cds;
CD** _cd = cds -> cd_array;
for ( int i = 0; i < numCDs; i++)
{
destroyCD (_cd[i]);
}
delete cds;
}
//
void displayCDs (CDs* cds)
{
int numAlbum = cds -> num_cds;
CD** _cd = cds -> cd_array;
for (int i = 0; i < numAlbum; i++)
{
cout << cds -> cd_array[i] << endl;
cout << displaySong (song);
}
}
//
CDs* findCDs (CDs* cds, string* artist)
{
CD* compact = NULL;
int numAlbum = cds -> num_cds;
CD** _cd = cds -> cd_array;
for (int i = 0; i < numAlbum; i++)
{
if(artist == _cd )
{
_cd[i] = cds -> num_cds[i];
break;
}
}
return _cd;
}
/*
*/
#ifndef CDS_STRUCT
#define CDS_STRUCT
#include "CD.h"
using namespace std;
struct CDs
{
CD** cd_array;
int num_cds;
int max_cds;
};
//
void resize(CDs* cds);
//
CDs* createCDs(string filename, fstream &file );
//
void destroyCDs(CDs* cds);
//
void displayCDs(CDs* cds);
//
CDs* findCDs(CDs* cds, string* artist);
#endif
/*
*/
#include "CD.h"
#include <iostream>
using namespace std;
//
CD* createCD (string* artist, string* title, int year, int rating, int num_tracks)
{
CD* newCD = new CD;
newCD -> artist = artist;
newCD -> title = title;
newCD -> year = year;
newCD -> rating = rating;
newCD -> num_tracks = num_tracks;
newCD -> track_count = 0;
newCD -> song_array = new Song*[num_tracks];
return newCD;
}
//
void addSong (CD* cd, string* title, string* length)
{
Song* song = createSong (title, length);
int songNum = cd->track_count;
cd -> song_array[songNum] = song;
cd -> track_count++;
}
//
void destroyCD (CD* cd)
{
int songNum = cd->num_tracks;
Song** cdSongs = cd->song_array;
for ( int i = 0; i < songNum; i++)
{
destroySong (cdSongs[i]);
}
delete cd;
}
/*
*/
#ifndef CD_STRUCT
#define CD_STRUCT
#include <string>
#include "Song.h"
using namespace std;
struct CD
{
string* artist;
string* title;
int year;
int rating;
int num_tracks;
int track_count;
Song** song_array;
};
//
CD* createCD(string* artist, string* title, int year, int rating,int num_tracks);
//
void addSong(CD* cd, string* title, string* length);
//
void destroyCD(CD* cd);
#endif
/*
*/
#include "Song.h"
#include <iostream>
using namespace std;
//
Song* createSong (string* title, string* length)
{
Song* newSong = new Song;
newSong -> title = title;
newSong -> length = length;
return newSong;
}
//
void displaySong (Song* song)
{
cout << song -> title << "\t" << song -> length << endl;
}
//
void destroySong (Song* song)
{
delete song;
}
/*
*/
#include "Song.h"
#include <iostream>
using namespace std;
//
Song* createSong (string* title, string* length)
{
Song* newSong = new Song;
newSong -> title = title;
newSong -> length = length;
return newSong;
}
//
void displaySong (Song* song)
{
cout << song -> title << "\t" << song -> length << endl;
}
//
void destroySong (Song* song)
{
delete song;
}
Last edited on
Why are your structures storing pointers for the text fields? That just makes a memory management nightmare.
You're managing your own expandable arrays for the CDs and songs. Why not use vector<> instead? It will enormously simplify your code.