Sorting and Display

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//playlist.h:

#ifndef _PLAYLIST_H_
#define _PLAYLIST_H_
#include <iostream>
#include "song.h"
using namespace std;

class Playlist
{
public:
    Playlist();
    ~Playlist();
    void AddSong(const Song& s);
    void DeleteSong(const Song& s);
    void Show();
    
    
private:
    Song *playList;
    int size_of_playlist;
    int numberOfSongs;
    
};
#endif 

// playlist.cpp

#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
#include "playlist.h"

using namespace std;

Playlist::Playlist()
{
    size_of_playlist = 2;
    numberOfSongs = 0;
    playList = new Song[size_of_playlist];
}
Playlist::~Playlist()
{
    delete [] playList;
}
void Playlist::AddSong(const Song& s)
{
    if (numberOfSongs == size_of_playlist)
    {
        Song* arr = new Song[size_of_playlist+1];
        arr[size_of_playlist] = s;
        
        for(int i = 0; i < size_of_playlist; i++)
        {
            arr[i] = playList[i];
        }
        
        playList = arr;
        delete[] arr;
        arr = NULL;
    }
    else
    {
        numberOfSongs++;
        playList[numberOfSongs - 1] = s;
    }
    
}
void Playlist::DeleteSong(const Song& s)
{
    // First search specified song
    int index = 0;
    /*for (index = 0; index < numberOfSongs; index++)
    {
        if (playList[index] == s)
        {
            break;
        }
    }*/
    
    // Maybe not found
    if (index == numberOfSongs)
    {
        return;
    }
    
    // Remove song fron array
    while (index < (numberOfSongs-1))
    {
        playList[index] = playList[index+1];
        index++;
    }
    
    // We got one less song
    numberOfSongs--;
}
void Playlist::Show()
{
    Song s;
    if (numberOfSongs == size_of_playlist)
    {
        Song* arr = new Song[size_of_playlist+1];
        arr[size_of_playlist] = s;
        
        for(int i = 0; i < size_of_playlist; i++)
        {
            arr[i] = playList[i];
            cout << arr[i].GetTitle();
        }
    }
}

// song.h

#include <iostream>
using namespace std;

#ifndef _SONG_H
#define _SONG_H

enum Style {POP, ROCK, ALTERNATIVE, COUNTRY, HIPHOP, PARODY};

class Song
{
    // operator overload -- described at bottom
    friend ostream& operator<<(ostream& os, const Song& s);
    
public:
    Song();		// default constructor, sets up blank song object of <"", "", Pop, 0>
    
    void Set(const char* t, const char* a, Style st, int sz);

    const char* GetTitle() const;		// returns the title stored in the object
    const char* GetArtist() const;	// returns the artist
    int GetSize() const;			// returns the file size in kilobytes
    Style GetCategory() const;		// returns the song category
    
private:
    char title[36];	// may assume title is 35 characters or less
    char artist[21];	// may assume artist name is 20 characters or less
    Style category;	// style of the given song
    int size;		// file size, stored in kilobytes
};
/*
 
 Title		                       Artist              Style   Size (MB)
 
 Examples:
 Pictures of You                    The Cure            Alt          4.4
 Bohemian Rhapsody                  Queen               Rock         5.7
 What Does the Fox Say              Ylvis               Par         12.6
 
 */

#endif

// song.cpp

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
#include "song.h"

using namespace std;

Song::Song()
{
    strcpy(title, "");
    strcpy(artist, "");
    category = POP;
    size = 0;
}
void Song::Set(const char *t, const char *a, Style st, int sz)
{
    t = title;
    a = artist;
    st = category;
    sz = size;
}
const char* Song::GetTitle() const
{
    return title;
}
const char* Song::GetArtist() const
{
    return artist;
}
int Song::GetSize()const
{
    return size;
}
Style Song::GetCategory() const
{
    return category;
}
ostream& operator<<(ostream& os, const Song& s)
{
    os << s.title << setw(15) << s.artist << setw(15) << s.category << setw(10) << s.size << endl;
    return os;
}

// main.cpp

#include <iostream>
#include <iomanip>
#include <cctype>
#include "song.h"
#include "playlist.h"

using namespace std;

int main()
{
    int ss;
    char ti[36], ar[25];
    int sinput;
    Song s;
    Style g;
    Playlist f;
    char search[36];
    char input;
    do {
        cout << "------MENU------ " << endl;
        cout << "A: Add a song to the playlist" << endl;
        cout << "F: Find a song on the playlist" << endl;
        cout << "D: Delete a song from the playlist" << endl;
        cout << "S: Show the entire playlist" << endl;
        cout << "C: Category summary" << endl;
        cout << "Z: Show playlist size" << endl;
        cout << "M: Show this menu" << endl;
        cout << "X: Exit the program" << endl;
        cout << "Enter your input: ";
        cin >> input;
        
        if ( input == 'a' || input == 'A')
        {
            cin.ignore();
            cout << "Please enter the title: ";
            cin.get(ti, 36, '\n');
            cin.ignore();
            cout << "\nPlease enter the artist: ";
            cin.get(ar, 25, '\n');
            
            cout << "Please enter a category: ";
            cin >> sinput;
            g = static_cast<Style>(sinput);
            
            cout << "Please enter the size: ";
            cin >> ss;
            
            s.Set(ti, ar, g, ss);
        
        }
        else if ( input == 'f' || input == 'F')
        {
            cout << "Please enter a search string: ";
            cin >> search;
        }
        else if ( input == 'd' || input == 'D')
        {
            
        }
        else if ( input == 's' || input == 'S')
        {
            f.Show();
        }
        else if ( input == 'c' || input == 'C')
        {
            
        }
        else if ( input == 'z' || input == 'Z')
        {
        
        }
        else if ( input == 'm' || input == 'M')
        {
            cout << "Menu: " << endl;
            cout << "A: Add a song to the playlist" << endl;
            cout << "F: Fine a song on the playlist" << endl;
            cout << "D: Delete a song from the playlist" << endl;
            cout << "S: Show the entire playlist" << endl;
            cout << "C: Category summary" << endl;
            cout << "Z: Show playlist size" << endl;
            cout << "M: Show this menu" << endl;
            cout << "X: Exit the program" << endl;
            
            cout << "Please enter on the following letters: ";
            cin >> input;
        }
        else if (input == 'x' || input == 'X')
        {
            
        }
        else
        {
            cout << "Wrong input! Please try again..." << endl;
        }
        
        
    } while(input != 'X' || input != 'x');
    
    return 0;
}

for this code here I am having difficulty figuring out how to display all the songs that are entered in my playlist class, thus displaying the entire list. Also the delete function is a little tricky for me and I cant seem to get the commented out portion to work properly. Also if there is anything I may need to tweak on let me know
Last edited on
Your function Playlist::Show() appears to be creating a copy of your playlist, and showing that copy. Why? Just show the playlist.

That said, your add song function looks broken. It does this:

Create new array, slightly larger.
Copy the new song into the last element.
Copy the existing playlist into the other elements.
Make playlist point to the new array
Delete new array

I repeat that last bit; you're making the playlist point at the new array, and then you delete the new array, so playlist is pointing at garbage memory.

The following is advice to a beginner; you shouldn't be using new and delete. Those are not for beginners. You should be using a vector<Song>. You shouldn't be using char arrays; you should be using strings. You're coding in C++, and C++ provides these things for you so that you don't have to do the low level things yourself unless you really need to.

If your playlist was a vector<Song>, here is what your add song function would look like:

1
2
3
4
void addSong(const Song& s)
{
  playlist.push_back(s);
}


Yeah Im not supposed to use vector, i just want to know what I need to tweak for the Show() function since I cant remember how to dynamically allocated Song objects so i can print a list.
heres what I added to the Show() function but it still doesnt display:
1
2
3
4
5
6
7
void Playlist::Show()
{
    for (unsigned i = 0; i < numberOfSongs; i++)
    {
        cout << playList[i].GetTitle() << endl;
    }
}
Also can anyone explain to me what may be causing breaks in my code?
but it still doesnt display:

Did you fix your add song function so it doesn't throw away the entire playlist every time you add a song?
No unfortunately not, dont I need to just get rid of the deletion portion of the function because Im not sure if it will allocate without that portion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (numberOfSongs == size_of_playlist)
    {
        Song* arr = new Song[size_of_playlist+1];
        arr[size_of_playlist] = s;
        
        for(int i = 0; i < size_of_playlist; i++)
        {
            arr[i] = playList[i];
        }
        
        playList = arr;
        delete[] arr;
        arr = NULL;
    }
    else
    {
        numberOfSongs++;
        playList[numberOfSongs - 1] = s;
    }

You need to delete the OLD array. Your code deletes the new array.
So i just need to delete playList instead of arr
So I figured out what I was doing wrong but the code breaks at the end of the function so once I actually add the data to the list of array objects, the program ends.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Playlist::AddSong(const Song& s)
{
    if (numberOfSongs == size_of_playlist)
    {
        Song* arr = new Song[size_of_playlist+1];
        arr[size_of_playlist] = s;
        
        for(int i = 0; i < size_of_playlist; i++)
        {
            arr[i] = playList[i];
        }
        
        delete [] playList;
        
        playList = arr;
    }
}
while(input != 'X' || input != 'x'); this looks wrong to me.

it looks to me like this will ALWAYS be TRUE.

if it is 'X' then it is not 'x' so not 'x' is true and the OR puts that to true.
if it is 'z' then you get true or true = true.

I suspect you want && instead of ||
Topic archived. No new replies allowed.