Advertisement
Guest User

THREAD 1: EXC_BAD_ACCESS

a guest
Apr 3rd, 2020
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. main.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include "Song.h"
  6. #include "MusicLibrary.h"
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.  
  13.     string filename;
  14.     int numsongs;
  15.     char mode;
  16.  
  17.     cout << "Enter number of Songs " << endl;
  18.     cin >> numsongs;
  19.  
  20.     cout << "Enter filename with information about the songs" << endl;
  21.     cin >> filename;
  22.  
  23.     cout << "Enter mode of operation  (r : play Random, l: play PlayList, b: play both ) " << endl;
  24.     cin >> mode;
  25.  
  26.     MusicLibrary mylibrary(numsongs);
  27.     mylibrary.readSongsFromFile(filename);
  28.  
  29.     if (mode == 'r' || mode == 'b')
  30.     {
  31.         mylibrary.playRandom();
  32.     }
  33.  
  34.     if (mode == 'l' || mode == 'b')
  35.     {
  36.         for (int i = numsongs - 1; i >= 0; i--)
  37.         {
  38.             mylibrary.addSongToPlayList(i);
  39.         }
  40.  
  41.         mylibrary.playPlaylist();
  42.     }
  43.  
  44.     return 0;
  45. }
  46. ----------------------------------------------------------------------------------------------------------------------
  47. Song.cpp:
  48.  
  49. #include <iostream>
  50. #include "Song.h"
  51.  
  52. void Song::Play() {
  53.     cout << "Playing " << Title << " by " << Artist << " " << PlayTime << " seconds" << endl;
  54. }
  55.  
  56. Song::Song() {
  57.     PlayTime = 0;
  58.     Year = 0;
  59. }
  60.  
  61. Song::Song(string title, string artist, string album, int year, int playTime) {
  62.     Title = title;
  63.     Artist = artist;
  64.     Album = album;
  65.     Year = year;
  66.     PlayTime = playTime;
  67. }
  68.  
  69. void Song::setTitle(string title) {
  70.     Title = title;
  71. }
  72.  
  73. string Song::getTitle() {
  74.     return Title;
  75. }
  76.  
  77. void Song::setArtist(string artist) {
  78.     Artist = artist;
  79. }
  80.  
  81. string Song::getArtist() {
  82.     return Artist;
  83. }
  84.  
  85. void Song::setAlbum(string album) {
  86.     Album = album;
  87. }
  88.  
  89. string Song::getAlbum() {
  90.     return Album;
  91. }
  92.  
  93. void Song::setYear(int year) {
  94.     Year = year;
  95. }
  96.  
  97. int Song::getYear() {
  98.     return Year;
  99. }
  100.  
  101. void Song::setPlayTime(int playTime) {
  102.     PlayTime = playTime;
  103. }
  104.  
  105. int Song::getPlayTime() {
  106.     return PlayTime;
  107. }
  108.  
  109. bool Song::operator==(Song &rhs) {
  110.     return ((this->Title == rhs.Title) && (this->Artist == rhs.Artist) && (this->Album == rhs.Album));
  111. }
  112.  
  113. ------------------------------------------------------------------------------------------------------------------
  114. Song.h:
  115.  
  116. #pragma once
  117. #include <string>
  118. using namespace std;
  119.  
  120. class Song {
  121. private:
  122.     string Title;
  123.     string Artist;
  124.     string Album;
  125.     int Year;
  126.     int PlayTime;
  127.  
  128. public:
  129.     Song();
  130.     Song(string title, string artist, string album, int year, int playTime);
  131.     void setTitle(string title);
  132.     string getTitle();
  133.     void setArtist(string artist);
  134.     string getArtist();
  135.     void setAlbum(string album);
  136.     string getAlbum();
  137.     void setYear(int year);
  138.     int getYear();
  139.     void setPlayTime(int playTime);
  140.     int getPlayTime();
  141.     bool operator==(Song &rhs);
  142.     void Play();
  143. };
  144. ---------------------------------------------------------------------------------------------------------------------
  145. MusicLibrary.cpp:
  146.  
  147. #include <iostream>
  148. #include <fstream>
  149. #include <sstream>
  150. #include <string>
  151. #include "MusicLibrary.h"
  152.  
  153. MusicLibrary::MusicLibrary(int maxsongs) {
  154.     maxSongs = maxsongs;
  155.     mySongs = new Song[maxSongs];
  156.     playList = &mySongs;
  157.     numSongs = 0;
  158.     numSongsPlayList = 0;
  159. }
  160. MusicLibrary::MusicLibrary(MusicLibrary &other) {
  161.     maxSongs = other.maxSongs;
  162.     numSongs = other.numSongs;
  163.     mySongs = other.mySongs;
  164.     numSongsPlayList = other.numSongsPlayList;
  165.     playList = other.playList;
  166. }
  167.  
  168. MusicLibrary::~MusicLibrary() {
  169.     delete[] mySongs;
  170.     delete[] playList;
  171. }
  172.  
  173. int MusicLibrary::getnumSongs() {
  174.     return numSongs;
  175. }
  176. int MusicLibrary::getmaxSongs() {
  177.     return maxSongs;
  178. }
  179. int MusicLibrary::getnumSongsPlayList() {
  180.     return numSongsPlayList;
  181. }
  182.  
  183. bool MusicLibrary::addSong(string title, string artist, string album, int year, int time) {
  184.     if (numSongs == maxSongs) {
  185.         cout << "Could not add song to library. Library is full" << endl;
  186.         return false;
  187.     }
  188.     mySongs[numSongs].setTitle(title);
  189.     mySongs[numSongs].setArtist(artist);
  190.     mySongs[numSongs].setAlbum(album);
  191.     mySongs[numSongs].setYear(year);
  192.     mySongs[numSongs].setPlayTime(time);
  193.     numSongs++;
  194.     cout << numSongs << " Added " << mySongs[numSongs].getTitle() << endl;
  195.     return true;
  196. }
  197.  
  198. bool MusicLibrary::addSong(Song &song) {
  199.     if (numSongs == maxSongs) {
  200.         cout << "Could not add song to library. Library is full" << endl;
  201.         return false;
  202.     }
  203.     mySongs[numSongs] = song;
  204.     numSongs++;
  205.  
  206.     return true;
  207. }
  208.  
  209. void MusicLibrary::readSongsFromFile(string filename) {
  210.  
  211.     ifstream input;
  212.     input.open(filename);
  213.     bool cont = true;
  214.  
  215.     if (input.is_open()) {
  216.         string line;
  217.         while (getline(input, line) && cont) {
  218.             string title, artist, album;
  219.             string s_year, s_time;
  220.             int year;
  221.             int time;
  222.             istringstream inSS(line);
  223.  
  224.             getline(inSS, title, ',');
  225.             getline(inSS, artist, ',');
  226.             getline(inSS, album, ',');
  227.             getline(inSS, s_year, ',');
  228.             getline(inSS, s_time);
  229.  
  230.             year = stoi(s_year);
  231.             time = stoi(s_time);
  232.             cout << title << endl;
  233.             cont = addSong(title, artist, album, year, time);
  234.         };
  235.     }
  236.     else {
  237.         cout << "could not open file " << filename << endl;
  238.     }
  239. }
  240.  
  241. void MusicLibrary::playRandom() {
  242.     int x = 0;
  243.     int y = numSongs - 1;
  244.     while(x > y) {
  245.         mySongs[x].Play();
  246.         mySongs[y - x].Play();
  247.         x++;
  248.         y--;
  249.     }
  250. }
  251.  
  252. bool MusicLibrary::addSongToPlayList(int pos) {
  253.     if(numSongsPlayList == maxSongs) {
  254.         cout << "Playlist is full" << endl;
  255.         return false;
  256.     }
  257.  
  258.     if((pos < 0) || (pos > numSongs)) {
  259.         cout << "Invalid song" << endl;
  260.         return false;
  261.     }
  262.  
  263.     numSongsPlayList = numSongsPlayList + 1;
  264.     *playList[numSongsPlayList] = mySongs[pos];
  265.     return true;
  266. }
  267.  
  268. void MusicLibrary::playPlaylist()
  269. {
  270.     for(int i = 0; i < numSongsPlayList; i++) {
  271.         playList[i]->Play();
  272.     }
  273. }
  274. -------------------------------------------------------------------------------------------------------------------------
  275. MusicLibrary.h:
  276.  
  277. #pragma once
  278. #include <string>
  279. #include "Song.h"
  280.  
  281. using namespace std;
  282.  
  283. class MusicLibrary {
  284. private:
  285.     int maxSongs;
  286.     int numSongs;  // number of Songs in library
  287.     Song *mySongs; // dynamic array storing all Songs
  288.  
  289.     int numSongsPlayList; // number of Songs in Playlist
  290.     Song **playList;      // dynamic array of pointers to Songs
  291.  
  292. public:
  293.     MusicLibrary(int maxsongs);
  294.     MusicLibrary(MusicLibrary &other);
  295.     ~MusicLibrary();
  296.  
  297.     int getnumSongs();
  298.     int getmaxSongs();
  299.     int getnumSongsPlayList();
  300.  
  301.     bool addSong(string title, string artist, string album, int year, int time);
  302.     bool addSong(Song &song);
  303.     void readSongsFromFile(string filename);
  304.  
  305.     bool addSongToPlayList(int pos);
  306.     void playRandom();
  307.     void playPlaylist();
  308. };
  309. --------------------------------------------------------------------------------------------------------------------------
  310. songs.txt:
  311.  
  312. Zoo Station, U2, Achtung Baby, 1991, 203
  313. Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
  314. Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
  315. Summer of 69, Bryan Adams, Reckless, 1984, 178
  316. Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement