findCDs function help

in the first file how would the findCDs function be written correctly? it's supposed to check what the user typed in with the cds stored. If the user typed in a valid name then it returns something.

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
265
266
267
268
269
270
271
272
273
274
275
276
#include "CDs.h"
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>

using namespace std;


//
CDs* createCDs(const char* filename,fstream &file)
{
	
	const char* _file;
	int max = 1;
	int min = 0;
	CD** cdPtrArray = new CD*[max];
	CDs* _cds = new CDs;
	
	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;
		if (min == max)
		{
			resize(_cds);
		}
		
		CD* c = createCD(artist, title, year, rating, num_tracks);
		cdPtrArray[min] = c;
		
		for (int i = 0; i < num_tracks; i++)
		{
			file >> length[0];
			file >> title[0];
		}
		min++;
	}	
	
	_cds -> num_cds = min;
	_cds -> max_cds = min;
	_cds -> cd_array = cdPtrArray;
	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 << _cd[i] << endl;
	}
	
}

//
void resize (CDs* cds)
{
	int cd_nums = cds -> num_cds;
	int cd_max = cds -> max_cds;
	CD** cd = cds -> cd_array;
	CD** cdPtr = new CD*[cd_max * 2];
	for (int i = 0; i < cd_nums; i++)
	{
		cd[i] = cdPtr[i];
	}
	delete cd;
	
	cd_max * 2;
}

//
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 == compact )
		{
			_cd[i] = compact;
			break;
		}
	}
	return cds;
}

#ifndef CDS_STRUCT
#define CDS_STRUCT

#include "CD.h"

using namespace std;

struct CDs
{
	CD** cd_array;
	int num_cds;
	int max_cds;
};



//
CDs* createCDs(string filename, fstream &file );

//
void destroyCDs(CDs* cds);

//
void displayCDs(CDs* cds);

//
void resize(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;
}


#ifndef SONG_STRUCT
#define SONG_STRUCT

#include <string>

using namespace std;

struct Song
{
	string* title;
	string* length;
};

//
Song* createSong(string* title, string* length);

//
void displaySong(Song* song);

//
void destroySong(Song* song);

#endif 
Firstly, please stop spamming the forum with multiple threads for the same thing. It's annoying, and it wastes peoples' time, because they answer on one thread, without realising someone else may have answered on another.

Your findCDs function is a bit of a mess. You never set the value of compact to be anything, so it's always NULL, but you try and use it to compare with artist. Also, compact is a pointer to a CD, while artist is a pointer to a string, so even if CD was pointing to something valid, it couldn't possibly be a sensible comparison.

You seem to be confused as to what cds represents. At lines 106 and 109, you use it as if it's the existing database of CD's, in which you're trying to find an artist. But at lines 107 and 114, you're using it as if it was an object for storing the results of the search. Which is it?

To find a list of CD's of a given artist, here's how I would do it:

1) Your function needs to take as read-only (i.e. const) arguments, a pointer to the database of CDs, and the name of the artist.

2) Your function needs to iterate over all the CD's in the database, comparing the artist of each CD to the artist passed in as an argument.

3) If the artist of any CD matches the argument, store a pointer to the CD in a separate object containing the results. A vector of pointers to CD would be a good choice here,

4) Pass that object containing the results back to the calling code - either by returning it, or using a third argument that's passed by reference.

EDIT: dlhayden's comments on one of your duplicate threads are also relevant here.
Last edited on
Topic archived. No new replies allowed.