Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**/
- #include <iostream>
- #include <cstring>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- const int NO_OF_SONGS = 21;
- struct songInfo
- {
- char title[30];
- char artist[30];
- int durationMin = 0;
- int durationSec = 0;
- char album[30];
- };
- void addSong (songInfo list[], int listSize);
- void removeSong (songInfo list[], int listSize);
- void displayList (songInfo list[], int listSize);
- void searchList (songInfo list[], int listSize);
- void readList (ifstream& infile, songInfo list[], int listSize);
- void printList (ofstream& outfile, songInfo list[], int listSize);
- int main()
- {
- songInfo trackListing[NO_OF_SONGS];
- char selection;
- cout << endl;
- cout << "*Music Track Database*" << endl;
- do
- {
- ifstream infile("songs.txt");
- ofstream outfile("songs.txt");
- if (!infile)
- {
- cout << "Cannot open file: songs.txt";
- }
- cout << "Main Menu:" << endl;
- cout << endl;
- cout << "(a)dd a song" << endl;
- cout << "(r)emove a song" << endl;
- cout << "(d)isplay track listings" << endl;
- cout << "(s)earch for a track" << endl;
- cout << "(q)uit" << endl;
- cout << endl;
- cout << "Select (a, r, d, s, q): ";
- cin >> selection;
- cout << endl;
- if (selection != 'a' && selection != 'r' && selection != 'd' && selection != 's' && selection != 'q')
- {
- cout << "Select (a, r, d, s, q): ";
- cin >> selection;
- cout << endl;
- }
- switch (selection)
- {
- case 'a':
- readList(infile, trackListing, NO_OF_SONGS);
- addSong(trackListing, NO_OF_SONGS);
- printList(outfile, trackListing, NO_OF_SONGS);
- break;
- case 'd':
- readList(infile, trackListing, NO_OF_SONGS);
- displayList(trackListing, NO_OF_SONGS);
- break;
- case 'r':
- removeSong(trackListing, NO_OF_SONGS);
- printList(outfile, trackListing, NO_OF_SONGS);
- break;
- case 's':
- readList(infile, trackListing, NO_OF_SONGS);
- searchList(trackListing, NO_OF_SONGS);
- break;
- }
- }while (selection != 'q');
- cout << "Happy Trails To You; Until We Meet Again!" << endl;
- cout << endl;
- return 0;
- }
- void addSong (songInfo list[], int listSize)
- {
- int i;
- cout << "Add a Song" << endl;
- cout << endl;
- cout << "Enter Song Number in list: ";
- cin >> i;
- cout << endl;
- cout << "Enter Song Title: ";
- cin.ignore();
- cin.get(list[i].title, 30);
- cout << endl;
- cout << "Enter Artist Name: ";
- cin.ignore();
- cin.get(list[i].artist, 30);
- cout << endl;
- cout << "Enter Track Duration: ";
- cout << endl;
- cout << "(Seperate mins & secs with a space.";
- cout << endl;
- cout << "ex: if 3:40, enter 3 40): ";
- cin >> list[i].durationMin >> list[i].durationSec;
- cout << endl;
- cout << "Enter Album Title: ";
- cin.ignore();
- cin.get(list[i].album, 30);
- cout << endl;
- cout << "Song Added to Database!";
- cout << endl;
- cout << endl;
- }
- void readList (ifstream& infile, songInfo list[], int listSize)
- {
- int i;
- infile.open("songs.txt");
- while(infile)
- {
- for(i=1;i<listSize;i++)
- {
- infile.get(list[i].title, 30);
- infile.get(list[i].artist, 30);
- infile >> list[i].durationMin >> list[i].durationSec;
- infile.get(list[i].album, 30);
- }
- }
- infile.close();
- }
- void printList (ofstream& outfile, songInfo list[], int listSize)
- {
- outfile.open("songs.txt");
- int i;
- for (i=1; i<listSize; i++)
- {
- outfile << left << setw(2) << i << left << setw(32) << list[i].title << ';' << left << setw(32) << list[i].artist << ';' << right << setw(4) << list[i].durationMin << ';' << left << setw(5) << list[i].durationSec << ';' << left << setw(32) << list[i].album << ';' << endl;
- }
- outfile.close();
- }
- void removeSong (songInfo list[], int listSize)
- {
- int i=1;
- int e;
- cout << "Remove a Song" << endl;
- cout << "Enter Track Number to Confirm Deletion: ";
- cin >> e;
- cout << endl;
- for (i=1; i<listSize; i++)
- {
- if (e==i)
- {
- for(i=e; i<listSize; i++)
- {
- list[i] = list[i+1];
- }
- }
- }
- }
- void displayList (songInfo list[], int listSize)
- {
- int i;
- cout << left << setw(14) << '#' << left << setw(21) << "Song Name" << left << setw(21) << "Artist Name" << left << setw(20) << "Duration" << left << setw(22) << "Album Title" << endl;
- cout << left << setw(110) << setfill ('-') << '-' << endl;
- cout << setfill (' ');
- for (i=1; i<listSize; i++)
- {
- cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
- }
- cout << left << setw(110) << setfill ('-') << '-' << endl;
- cout << setfill (' ');
- }
- void searchList (songInfo list[], int listSize)
- {
- char aOrB;
- int i = 1;
- char art[30];
- char alb[30];
- cout << "Search by (a)rtist or al(b)um? (a or b): ";
- cin >> aOrB;
- cout << endl;
- if (aOrB != 'a' && aOrB != 'b')
- {
- cout << "(a or b): ";
- cin >> aOrB;
- cout << endl;
- }
- if (aOrB == 'a')
- {
- cout << "Artist Name: ";
- cin.ignore();
- cin.get(art, 30);
- for(i=1; i<listSize; i++)
- {
- if (!strcmp(art, list[i].artist))
- {
- cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
- }
- }
- }
- else
- {
- cout << "Album Name: ";
- cin.ignore();
- cin.get(alb, 30);
- for(i=1; i<listSize; i++)
- {
- if (!strcmp(alb, list[i].album))
- {
- cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
- }
- else
- cout << "Album not found" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement