Make a playlist with the music info input by the user

closed account (zq4NT05o)
DJ Playlist Manager:
Last edited on
Store your songs and playlists in std::vector. This doesn't restrict your lists to a limited size.
Below I have made a short example which shows you how to handle your strucs.
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
#include <iostream>
#include <vector>
#include <string>

struct Song
{
    std::string title;
    std::string artist;
    double length;
};

struct Playlist
{
    std::string title;
    std::string artist;
    std::string genre;
    double length;
    std::vector<Song*> songList;
    void addSong( Song * song ) {
        songList.push_back( song );
        length += song->length;
    }
};

std::vector<Song> mySongs;
std::vector<Playlist> myPlaylists;
int main()
{
    // how to add a song into mySongs:

       // 1) create a song-object
    std::string title, artist, length;
    std::getline(std::cin, title);   // getline reads a whole line
    std::getline(std::cin, artist);
    std::getline(std::cin, length);
    Song song;
    song.title = title;
    song.artist = artist;
    song.length = std::stod(length);
      // 2) store your song-object
    mySongs.push_back( song );

    // how to add a song to a Playlist
    Playlist myPlaylist;
    myPlaylist.addSong( &song );   // uses the song object created above

    // how to show the song's data in a playlist:
    for (auto song : myPlaylist.songList)
    { 
        std::cout << "title:"
                  << song->title
                  << " artist:"
                  << song->artist
                  << " length: "
                  << song->length
                  << std::endl;
    }
}

I hope this helps you understanding how structs get filled and stored.
Consider that here in this example all songs are stored in a std::vector<Songs> container. In a std::vector<Playlist> container there will merely stored pointers to the songs.

If still something is unclear to you, don't hesitate to ask.
closed account (zq4NT05o)
Wow thanks so much!
Last edited on
closed account (zq4NT05o)
Ive updated my code with a class for song. Not sure how to store those songs added into a playlist.
Last edited on
Hints:
Song.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
#ifndef MY_SONG_CLASS
#define MY_SONG_CLASS


#include <string>

class Song {
private:
public:
    //member variables
    std::string songTitle;
    std::string artistName;
    std::string songGenre;
    double songLength {0.0};

    // functions
    Song();
    Song(const std::string& title, const std::string& artist, 
         const std::string& genre, double length);

    // Sets a name for the song
    void setSongTitle(const std::string& st);
    
    // Sets the artist name of the song
    void setArtistName(const std::string& ar);

    // Sets the album name of the song
    void setSongGenre(const std::string& sg);

    //Sets the song length of the song
    void setSongLength(double sl);

    // Gets the name of the song
    std::string getSongName();

    // Gets the name of the artist
    std::string getArtistName();

    // Gets the album name of the song
    std::string getSongGenre();

    //Gets the song length of the song
    double getSongLength();
};


#endif // MY_SONG_CLASS 


Song.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
#include "Song.h"

// Default constructor
/* --------------------------------------------------------------------------
 * Note: you don't need to initialize std::strings.
 * std::string is an object which is initialized as an empty string by its
 * own constructor.
 * -------------------------------------------------------------------------- */
Song::Song()
{}

Song::Song(const std::string& title, const std::string& artist, 
           const std::string& genre, double length)
{
    songTitle = title;
    artistName = artist;
    songGenre = genre;
    songLength = length;
}

void Song::setSongTitle(const std::string& st)
{
    songTitle = st;
}


void Song::setArtistName(const std::string& ar)
{
    artistName = ar;
}


void Song::setSongGenre(const std::string& sg)
{
    songGenre = sg;
}


void Song::setSongLength(double sl)
{
    songLength = sl;
}

std::string Song::getSongName()
{
    return songTitle;
}

std::string Song::getArtistName()
{
    return artistName;
}

std::string Song::getSongGenre()
{
    return songGenre;
}

double Song::getSongLength()
{
    return songLength;
}


Playlist.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef MY_PLAYLIST
#define MY_PLAYLIST

#include <string>
#include <vector>
#include "Song.h"

class Playlist {
public:
    Playlist(const std::string& name_arg = "default");
    void addSong(Song&& song);
    friend std::ostream& operator<<(std::ostream& os, const Playlist& value);
private:
    std::string name;
    double length {0.0};
    std::vector<Song> song_list;
};


#endif // MY_PLAYLIST 


Playlist.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
#include <iostream>
#include "Playlist.h"

Playlist::Playlist(const std::string& name_arg)
    : name {name_arg}
{}


void Playlist::addSong(Song&& song)
{
    length += song.songLength;
    song_list.push_back(song);
}

/* --------------------------------------------------------------------------
 *                    FRIEND FUNCTIONS
 * -------------------------------------------------------------------------- */
std::ostream& operator<<(std::ostream& os, const Playlist& value)
{
    os << "Playlist '" << value.name << "':\n";
    for(const auto& s : value.song_list) {
        os << "title: " << s.songTitle << "; artist: " << s.artistName
           << "; genre: " << s.songGenre << "; duration " << s.songLength 
           << '\n';
    }
    return os;
}


main.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
138
139
140
141
#include <cctype>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string> 
#include <vector>
#include "Playlist.h"
#include "Song.h"

Playlist& askForSongs(Playlist& play);
std::vector<Playlist>& addNewPlaylist(std::vector<Playlist>& play);
void displayAllPlaylists(const std::vector<Playlist>& plays);
int exitMenu();
void waitForEnter();

// Main Menu
int main()
{
    std::vector<Playlist> to_play(1); // at least 1 default element
    int choice = 0;
    do {
        std::cout << "\n***************************************************\n"
                     "*                                                 *\n"
                     "*             Playlist Manager                    *\n"
                     "*                                                 *\n"
                     "***************************************************\n"
                     "Where would you like to start? \n\n"
                     "1. Add Songs\n"
                     "2. Add New Playlist\n"
                     "3. View All Playlists \n"
                     "4. Exit\n\n"
                     "Enter Your Choice: ";
        std::cin >> choice;
        std::cin.ignore(1);
        
        // Set the numeric output formatting.
        std::cout << std::fixed << std::showpoint << std::setprecision(2);

        // Respond to the user's menu selection.
        switch(choice) {
        case 1:     // Add songs to a playlist
            askForSongs(to_play.at(0));
            break;
        case 2:
            addNewPlaylist(to_play);
            break;
        case 3:
            displayAllPlaylists(to_play);
            break;
        case 4:
            std::cout << "Happy Listening!\n";
            choice = exitMenu();
            break;
        default:
            break;
        }
    } while (0 < choice && choice < 4);
    waitForEnter();
    return 0;
}

Playlist& askForSongs(Playlist& play)
{
    std::string title;
    do {
        int i = 1;
        std::cout << "\nInsert new song into playlist"
                     " - leave title empty to end insertion."
                     "\nEnter title of song# " << i << ": ";
        std::getline(std::cin, title);
        if(title.empty()) { break; }

        std::cout << "Enter artist for this song: ";
        std::string artist;
        std::getline(std::cin, artist);
        
        std::cout << "Enter genre for this song: ";
        std::string genre;
        std::getline(std::cin, genre);
        
        std::cout << "Enter duration (in mins) for this song: ";
        double song_duration = 0.0;
        std::cin >> song_duration;
        std::cin.ignore(1);

        std::cout << "\nAdding " << genre << " song \"" << title 
                  << "\" by " << artist << ". Running time: " 
                  << song_duration << " mins \n";
        i++;
        Song song(title, artist, genre, song_duration);
        play.addSong(std::move(song));
    } while (not title.empty());
    return play;
}

std::vector<Playlist>& addNewPlaylist(std::vector<Playlist>& play)
{
    std::cout << "\nEnter Playlist Name: ";
    std::string n_playlist;
    std::getline(std::cin, n_playlist);
    Playlist just_created(n_playlist);

    std::cout << "\nYour new playlist "<<  n_playlist  << " has been added! \n\n";
    char option = 'n';
    do {
        std::cout << "Would you like to add songs to your new playlist "
                   <<  n_playlist  <<"? (Y/N) ";
        std::cin >> option;
        std::cin.ignore(1);
        option = std::toupper(option);
    } while (option not_eq 'Y' and option not_eq 'N');
    
    if(option == 'Y') { askForSongs(just_created); }
    
    play.push_back(just_created);

    return play;
}

void displayAllPlaylists(const std::vector<Playlist>& plays)
{ for(const auto& a : plays) { std::cout << a; } }

//Exit Menu
int exitMenu()
{
    std::cout << "\n\n";
    std::cout << "Would you like to exit the program? (Y/N) ";
    char answ = 'N';
    std::cin >> answ;
    std::cin.ignore(1);
    if(answ == 'Y' || answ == 'y') {
        return 4;
    }
    return 3; // == show playlists
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.