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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function Prototypes:
void addAlbum();
void displayAlbums();
bool deleteAlbum(string deletedAlbum, bool success);
int main()
{
ifstream fin;
ofstream fout;
bool success = false;
string deletedAlbum;
string albumTitle;
int choice = 0;
//Main Menu of Program:
cout << "Welcome to the music storage program. Here you can keep track of your favorite albums, artists and release years. What would you like to do?" << endl;
do
{
cout << "1: Display Favorite Albums" << endl;
cout << "2: Add an Album" << endl;
cout << "3: Remove an Album" << endl;
cout << "4: Exit" << endl;
cin >> choice;
if(choice == 1)
displayAlbums();
//Displays albums on screen, to the user.
if(choice == 2)
addAlbum();
//Adds an album that the user may enter into the program.
if(choice == 3)
//Asks user to enter the title of the album they wish to delete.
{
cout << "Enter an album to remove: "<< endl;;
cin >> deletedAlbum;
success = deleteAlbum(deletedAlbum, success);
if(success == true)
cout << "Album deleted." << endl;
else
cout << "Could not delete. I guess you can't get that one song out of your head." << endl;
}
} while(choice != 4);
return 0;
}
//Name: displayAlbums
//Description: Displays on screen the contents of "myFavoriteAlbums.txt".
//Precondition: Text file must have been created ("myFavoriteAlbums.txt"), complete with variables such as year of release, album artist and title.
void displayAlbums()
{
string title;
string artist;
int yearReleased;
ifstream fin;
fin.open("myFavoriteAlbums.txt");
if(fin.is_open())
{
while(isalnum(fin.peek()))
//Display of albums to user
{
getline(fin, title);
getline(fin, artist);
fin >> yearReleased;
fin.ignore(5, '\n');
cout << title << " by " << artist << " released in " << yearReleased << "." << endl;
}
fin.close();
}
else //if the file open is unsuccessful
{
cout << "File cannot be opened." << endl;
}
}
//Name: deleteAlbum
//Description: Deletes album data from program.
//Input: string deletedAlbum
// bool success - determines if deleted successfully
//Output: bool success - determines if deleted successfully
bool deleteAlbum (string deletedAlbum, bool success)
{
ifstream fin;
ofstream fout;
const int ARRAY_SIZE = 100;
string albumTitle [ARRAY_SIZE] = {" "};
string albumArtist [ARRAY_SIZE] = {" "};
int yearOfRelease [ARRAY_SIZE] = {0};
int counter = 0;
string title, artist;
int yearReleased;
fin.open("myFavoriteAlbums.txt");
if(fin.is_open())
{
while(isalnum(fin.peek()) && counter < ARRAY_SIZE)
{
getline(fin, title);
if(title != deletedAlbum)
{
albumTitle [counter] = title;
getline(fin,artist);
albumArtist[counter] = artist;
fin >> yearReleased;
fin.ignore(5, '\n');
yearOfRelease[counter] = yearReleased;
counter++;
}
else
{
getline(fin, artist);
fin >> yearReleased;
fin.ignore(5, '\n');
success = true;
}
}
fin.close();
}
else
cout << "File cannot be read." << endl;
fout.open("myFavoriteAlbums.txt"); //Overwrites data in file and program.
if(fout.is_open())
{
for(int i = 0; i < counter; i++)
{
fout << albumTitle[i] << "\n";
fout << albumArtist[i] << endl;
fout << yearOfRelease[i] << "\n";
}
fout.close();
}
else
cout << "Cannot read file!" << endl;
return success;
}
//Name: addAlbum
//Description: Adds user-inputted album to "myFavoriteAlbums.txt".
void addAlbum ()
{
ofstream fout;
ifstream fin;
string albumTitle;
string albumArtist;
int yearOfRelease;
fout.open("myFavoriteAlbums.txt", ios::app);
if(fout.is_open())
{
cin.ignore(5, '\n');
cout << "Enter the title of the album: ";
getline(cin, albumTitle);
fout << albumTitle << endl;
cout << "Enter the artist who crafted the gem of an album: ";
getline(cin, albumArtist);
fout << albumArtist << endl;
cout << "Enter the year the album was released: ";
cin >> yearOfRelease;
cin.ignore(100, '\n');
fout << yearOfRelease << endl;
fout.close();
cout << endl;
}
else
{
cout << "We can't input that album. You either entered in Justin Bieber, or there is an error." << endl;
}
(fin.close());
}
|