In: Computer Science
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Song {
public:
void SetNameAndDuration(string songName, int songDuration) {
name = songName;
duration = songDuration;
}
void PrintSong() const {
cout << name << " - " << duration <<
endl;
}
string GetName() const { return name; }
int GetDuration() const { return duration; }
private:
string name;
int duration;
};
int main() {
vector<Song> songPlaylist;
Song currentSong;
string currentName;
int currentDuration;
unsigned int i;
cin >> currentName;
while (currentName != "quit") {
/* Your code goes here */
}
for (i = 0; i < songPlaylist.size(); ++i) {
currentSong = songPlaylist.at(i);
currentSong.PrintSong();
}
return 0;
}
#include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name; } int GetDuration() const { return duration; } private: string name; int duration; }; int main() { vector<Song> songPlaylist; Song currentSong; string currentName; int currentDuration; unsigned int i; cin >> currentName; while (currentName != "quit") { cin >> currentDuration; currentSong.SetNameAndDuration(currentName, currentDuration); songPlaylist.push_back(currentSong); cin >> currentName; } for (i = 0; i < songPlaylist.size(); ++i) { currentSong = songPlaylist.at(i); currentSong.PrintSong(); } return 0; }