Playlist

Thanks for help in advance

So User should be able to input songs and sort them (title etc..)and at the end when the user doesn't want to check on more songs then I want the playlist to print out. like display This program missing a playlist func and piece of code that sort all in the end

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
  #include <iostream>
#include <string>
using namespace std;
 
struct song{
	string title;
	string artist;
	double duration;
};
 
int main(){
	song input[6];
	int x;
	string ans = "yes";
 cout<<"Enter 6 songs"<<endl;
	for(int i=0;i<5;i++){
    	cout<<i+1<<" Input format(title, artist, duration)";
        cin>>input[i].title>>input[i].artist>>input[i].duration;
	}
while(ans!="no"){
    	cout<<"Which songs (1-6) would you like to check on? ";
    	cin>>x;
        cout<<input[x-1].title<<" "<<input[x-1].artist<<", "<<input[x-1].duration<<endl;
    	cout<<"Would you like to check on song? (yes or no) ";
    	cin>>ans;
	}
return 0;}                             	
Last edited on
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct Song
{
    std::string m_title;
    std::string m_artist;
    double m_duration;

    Song(const std::string& title, const std::string& artist, const double duration)
        : m_title(title), m_artist (artist), m_duration(duration){}
    //http://en.cppreference.com/w/cpp/language/initializer_list
    bool operator < (const Song& rhs)
    //for sorting
    {
        return m_title < rhs.m_title;
    }
};
std::ostream& operator << (std::ostream& os, const Song& s)
//for printing
{
    os << s.m_title << " by " << s.m_artist << ". Running time: " << s.m_duration << " mins \n";
    return os;
}
int main()
{
    std::vector<Song> mySongs{};
    std::cout << "How many songs would you like to add: \n";
    size_t num{};
    std::cin >> num;
    std::cin.ignore();//ignores newline character in the input stream
    for (auto i = 0; i < num; ++i)
    {
        std::cout << "Enter title of song# " << i + 1 << ": \n";
        std::string title{};
        getline(std::cin, title);
        std::cout << "Enter artist for this song: \n";
        std::string artist{};
        getline(std::cin, artist);
        std::cout << "Enter duration (in mins) for this song: \n";
        double duration{};
        std::cin >> duration;
        std::cin.ignore();
        mySongs.emplace_back(Song(title, artist, duration));
        //http://en.cppreference.com/w/cpp/container/vector/emplace_back
    }
    std::sort(mySongs.begin(), mySongs.end());
    std::cout << "\nSorted (by title) song-list: \n\n";
    for (const auto& elem : mySongs)std::cout << elem;
}
Topic archived. No new replies allowed.