Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- //Song Class Declaration
- class Song
- {
- private:
- int time;
- int year;
- string title;
- string genre;
- string artist;
- public:
- Song();
- void set(string sname, string aname, string sgenre, int syear, int stime);
- void get(string &sname, string &aname, string &sgenre, int &syear, int &stime);
- void print();
- int gettime();
- void sort();
- string gettitle();
- };
- //***************************************************
- //* Class Constructor *
- //***************************************************
- Song::Song()
- {
- int time = 0;
- int year = 0;
- string title = "none";
- string genre = "none";
- string artist = "none";
- }
- //***************************************************
- //* Set Function *
- //***************************************************
- void Song::set(string sname, string aname, string sgenre, int syear, int stime)
- {
- time = stime;
- year = syear;
- title = sname;
- genre = sgenre;
- artist = aname;
- }
- //***************************************************
- //* Get Function *
- //***************************************************
- void Song::get(string &sname, string &aname, string &sgenre, int &syear, int &stime)
- {
- }
- //***************************************************
- //* Get Time Function *
- //***************************************************
- int Song::gettime()
- {
- return time;
- }
- //***************************************************
- //* Get Title Function *
- //***************************************************
- string Song::gettitle()
- {
- return title;
- }
- //***************************************************
- //* Sort Function *
- //***************************************************
- void Song::sort()
- {
- }
- //***************************************************
- //* Print Function *
- //***************************************************
- void Song::print()
- {
- using namespace std;
- cout << setw(15) << title << setw(15) << artist << setw(10) << genre << setw(6) << year << setw(5) << time << endl;
- }
- //***************************************************
- //* main function *
- //***************************************************
- int main()
- {
- fstream din;
- //Create an array
- int size = 0;
- cout << "Welcome to Heather's Song Data Program." << endl;
- cout << "When prompted, please enter up to 20 song data." << endl;
- cout << "When finished, hit Ctrl-D or it will auto stop at 20." << endl;
- Song array[size];
- //Initialize the array
- for (int i = 0; i <= 20; i++)
- {
- break;
- int time = 0;
- int year = 0;
- string genre, title, artist, junk;
- getline(cin, junk);
- cout << "Enter the name of the song." << endl;
- getline(cin, title);
- cout << "Enter the artist's name." << endl;
- getline(cin, artist);
- cout << "Enter the genre." << endl;
- getline(cin, genre);
- cout << "Enter the year it was released." << endl;
- cin >> year;
- cout << "Enter the time of the song in seconds." << endl;
- cin >> time;
- array[i].set(title, artist, genre, year, time);
- size++;
- }
- // Print array of Songs
- using namespace std;
- cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
- cout << "_________________________________________________"<< endl;
- for (int i = 0; i<size; i++)
- array[i].print();
- int minimum1;
- Song temp1;
- Song sortedtitle[size];
- //Initialize a second array for title sorting
- for (int i = 0; i <size; i++)
- {
- sortedtitle[i] = array[i];
- }
- //Access title to sort
- for (int i = 0; i <size-1; i++)
- {
- minimum1 = i;
- for (int j = 1+i; j <size; j++)
- {if (sortedtitle[j].gettitle() < sortedtitle[minimum1].gettitle())
- minimum1 = j;
- }
- if (minimum1 != i)
- {
- temp1 = sortedtitle[i];
- sortedtitle[i] = sortedtitle[minimum1];
- sortedtitle[minimum1] = temp1;
- }
- }
- // Print out title sorted array
- cout << endl;
- cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
- cout << endl;
- cout << "_________________________________________________"<< endl;
- for (int i = 0; i<size; i++)
- sortedtitle[i].print();
- int minimum;
- Song temp;
- //Initialize a third array for time sorting
- Song sortedtime[size];
- for (int i = 0; i < size; i++)
- {
- sortedtime[i] = array[i];
- }
- // Access time to sort
- // Sort the array using time in seconds
- for (int i = 0; i<size-1; i++)
- {
- minimum = i;
- for (int j = 1+i; j<size; j++)
- { if (sortedtime[j].gettime() < sortedtime[minimum].gettime())
- minimum = j;
- }
- if (minimum != i)
- { temp = sortedtime[i];
- sortedtime[i] = sortedtime[minimum];
- sortedtime[minimum] = temp;
- }
- }
- //Print sorted time array
- cout << endl;
- cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
- cout << endl;
- cout << "_________________________________________________"<< endl;
- for (int i = 0; i<size; i++)
- sortedtime[i].print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement