Adding data from class objects

Hello all, I am having difficulty figuring out why I cant correctly add the "size" which is my member data for a Song class. heres the code

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
#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();
    void Sort(char input);
    void Search(char t[35]);
    int Size();
    
private:
    Song *playList;
    int size_of_playlist;
    int numberOfSongs;
    
};

// func
int PLaylist::Size()
{
    for (int index = 0; index < numberOfSongs; index++)
    {
        
    }
}


so in my Song header file I have a member data int size. I made another class
called Playlist with the Size() function and Im trying to add the sizes of all the songs in that list of object arrays.
Last edited on
You haven't said what the error is, so I'm having to guess here.
 
int PLaylist::Size()
Here you wrote PLaylist, but the class you declared was Playlist.
1
2
3
4
5
6
7
8
9
int Playlist::Size()
{
    int sum_of_all_sizes = 0;
    for (int index = 0; index < numberOfSongs; index++)
    {
        sum_of_all_sizes += playList[i].size;   
    }
    return  sum_of_all_sizes;
}
Last edited on
oh alright I the code ran through but now there is a pointbreak in my code after the addSong function:
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;
    }
}


i can enter in all the data but once it is sent into a Set function in the Song class, the AddSong functions runs but breaks at the end.
What's the error message?
theres no error just the code wont continue just before the addsong function ends
So how do you know how far it got?
im using xcode so its telling me that it breaks at playlist = arr
Post the Song class.
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
#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);
    // the Set function should allow incoming data to be received through
    //  parameters and loaded into the member data of the object.  (i.e.
    //  this function "sets" the state of the object to the data passed in).
    //  The parameters t, a, st, and sz represent the title, artist, style,
    //  and size of the song file, respectively.
    
    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
};
Last edited on
Welp, I don't see anything. What error are you getting from the environment?
No error now I just cant seem to display the data from my Show function so I can print a list of Song objects through my Playlist class. So my guess is that there is something wrong with my add function since no data seems to be stored its just printing what is shown in the default constructor.
It doesn't seem that the members size_of_playlist and numberOfSongs are ever modified. By the way, is there any difference between these two members?
No, should I get rid of one of them that isnt used? I just need to know why my data isnt being displayed. I know the Add() function isnt working but I cant seem to figure out how to add user entered data from a main.cpp and store it into a Song object where I can then have that object stored in a playlist class and then list the data object by object.
Registered users can post here. Sign in or register to post.