Need some help

I am unsure how to clean up this code the way it needs to be.

This initial version of the program relies on treating each attribute of a song as a distinct, separate entity. Look at songcollection.h, both the structure and the parameter lists of the functions declared there, to see how awkward this gets.

Rewrite songcollection.h and songcollection.cpp to take advantage of the new Song type. You will need to replace the multiple arrays currently in a SongCollection by a single array of Songs. Then rewrite the functions for manipulating song collections accordingly. (Hopefully, you will see that this simplifies the code overall.)

songcollection.h:


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
 #ifndef SONGCOLLECTION_H

#define SONGCOLLECTION_H

#include <iostream>

#include <string>

#include "playlistitem.h"

#include "song.h"

#include "time.h"

const int MAXSONGS = 1000;

struct SongCollection {

int numSongs;

std::string titles[MAXSONGS];

std::string artists[MAXSONGS];

std::string albums[MAXSONGS];

int tracks[MAXSONGS];

int lengths[MAXSONGS];

Time totalTime;

};

/**

* Set up a new collection;

*/

void initialize (SongCollection& collection);

/**

* Add a single song to a collection.

*/

void addSong (SongCollection& toCollection,

       std::string title,

       std::string artist,

       std::string album,

       int track,

       Time length);

/**

* Add into one collection all songs from another collection that

* match a given artist and/or title.

*/

void addMatchesFor (const SongCollection& fromCollection,

      SongCollection& toCollection,

      std::string artist,

      std::string title);

/**

* Print a collection to the indicated output stream */ void print (std::ostream& out, const SongCollection& collection);

/**

* Attempt to read a collection of songs from an input stream,

* adding it to the collection if possible, */ bool read (std::istream& in, SongCollection& collection);

#endif 
  



songcollection.cpp:

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

 #include <iostream>

#include <string>

#include "songcollection.h"

using namespace std;

/**

* Set up a new collection;

*/

void initialize (SongCollection& collection) { collection.numSongs = 0; collection.totalTime = time(0,0,0); }

/**

* Add a single song to a collection.

*/

void addSong (SongCollection& toCollection,

std::string title,

std::string artist,

std::string album,

int track,

Time length)

{

int n = toCollection.numSongs;

toCollection.titles[n] = title;

toCollection.artists[n] = artist;

toCollection.albums[n] = album;

toCollection.tracks[n] = track;

toCollection.lengths[n] = length;

++toCollection.numSongs;

add(toCollection.totalTime, length);

}

/**

* Add into one collection all songs from another collection that

* match a given artist and/or title.

*/

void addMatchesFor (const SongCollection& fromCollection,

      SongCollection& toCollection,

      std::string artist,

      std::string title)

{

for (int i = 0; i < fromCollection.numSongs; ++i)

    {

      if (matches(artist, title,

    fromCollection.artists[i],

    fromCollection.titles[i]))

{

   addSong (toCollection, fromCollection.artists[i],

        fromCollection.titles[i], fromCollection.albums[i],

        fromCollection.tracks[i], fromCollection.lengths[i]); }

    }

}

void print (std::ostream& out, const SongCollection& collection) { for (int i = 0; i < collection.numSongs; ++i)

    {

      print (out, collection.titles[i], collection.artists[i],

      collection.albums[i], collection.tracks[i],

      collection.lengths[i]);

      out << endl;

    }

out << "Total: ";

print(out, collection.totalTime);

out << endl;

}

/**

* Attempt to read a song from an input stream, adding it

* to the collection if possible,

*/

bool read (std::istream& in, SongCollection& collection) { initialize(collection); string title, artist, album; int track; Time length; while (read(in, title, artist, album, track, length))

    {

      addSong(collection, title, artist, album, track, length);

    }

return in;

}
  

You didn't show us the new Song type so I have to guess a bit.

I would create the interface of the SongCollection like this.
1
2
3
4
5
6
7
8
9
10
11
12
struct SongCollection 
{
  SongCollection(); // replaces initialize
  Time getTotalTime();
  void AddSong(string title, string artist, string album, int track, Time length);
  void addMatchesFor (const SongCollection& fromCollection, string artist, string title);
  bool read (std::istream& in);
  void print (std::ostream& out);
private:
  int numSongs;
  Song songs[MAXSONGS];
};
Topic archived. No new replies allowed.