C++ I need to add the ability to save data to a disk in one or more files in my
ID: 3849025 • Letter: C
Question
C++
I need to add the ability to save data to a disk in one or more files in my program. The menu(s) should give the user the option to save or retrieve data.
//Please make sure the program runs in Microsoft Visual Studio
MY CODE:
#include <iostream>
#include <string>
using namespace std;
struct music
{
string title;
string artist;
string genre;
float len;
};
void menu(){
cout<<"Enter the corresponding key for the operation you want to do ";
cout<<"1. Manage Playlist ";
cout<<"2. Manage Songs ";
cout<<"3. Display Playlist ";
cout<<"4. Exit the program. ";
}
string manage_playlist(){
string pName;
cin.ignore();
cout << "Insert a name for this playlist: ";
getline (cin, pName);
cin.clear();
return pName;
}
void manage_songs(music arr_str[],int i){
for(int k=0;k<3;k++,i++)
{
cout << "Insert the title of the first song: ";
cin.ignore();
getline (cin, arr_str[i].title);
cout << "Insert the artist name of the first song: ";
cin.ignore();
getline (cin, arr_str[i].artist);
cout << "Insert the genre of the first song: ";
cin.ignore();
getline (cin, arr_str[i].genre);
cout << "Insert the length of the first song: ";
cin >> arr_str[i].len;
}
}
void display(music arr_str[],string pName,float totalLength){
{
string sTitle1, sTitle2, sTitle3,pName;
string sArtist1, sArtist2, sArtist3;
string sGenre1, sGenre2, sGenre3;
float sLength1, sLength2, sLength3;
float playlistAverage;
cout << "Choose your favorite genre" << endl;
cout << " 1 : " << sGenre1 << endl;
cout << " 2 : " << sGenre2 << endl;
cout << " 3 : " << sGenre3 << endl;
cout << " 4 : Exit program " << endl;
int choice = 0;
cout << "Your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout << "Wise!" << endl;
break;
case 2:
cout << "Rad!" << endl;
case 3:
cout << "Tubular!" << endl;
break;
case 4:
cout << "Exiting program..." << endl;
break;
}
playlistAverage = totalLength / 3;
for(int k=0;k<3;k++)
{
cout << "Name of the playlist: " << pName << " ";
cout << k+1 << ".-------------------------------------- ";
cout << " Title: " << arr_str[k].title << " ";
cout << " Artist: " << arr_str[k].artist << " ";
cout << " Genre: " << arr_str[k].genre << " ";
cout << " Length: " << arr_str[k].len << " ";
}
cout << "The playlist named " << pName << " has a total length of " << totalLength << " minutes. ";
cout << "Also, the playlist named " << pName << " has an average length of " << playlistAverage << " minutes ";
}
}
int main()
{
music arr_str[10];
string pName;
float totalLength = 0.0;
int newChoice=0, i=0, k=0;
cout << "############################################################ ";
cout << " Welcome to the DJ Dance Playlist Manager ";
cout << "############################################################ ";
while(1){
menu();
cin>>newChoice;
switch(newChoice) {
case 1:
pName = manage_playlist();
break;
case 2:
manage_songs(arr_str,i);
totalLength = totalLength + arr_str[i].len;
break;
case 3: display(arr_str,pName,totalLength);
break;
case 4: return 0;
}
}
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct music
{
string title;
string artist;
string genre;
float len;
};
void menu(){
cout<<"Enter the corresponding key for the operation you want to do ";
cout<<"1. Manage Playlist ";
cout<<"2. Manage Songs ";
cout<<"3. Display Playlist ";
cout<<"4.save the playlist";
cout<<"5. Exit the program. ";
}
string manage_playlist(){
string pName;
cin.ignore();
cout << "Insert a name for this playlist: ";
getline (cin, pName);
cin.clear();
return pName;
}
void manage_songs(music arr_str[],int i){
for(int k=0;k<3;k++,i++)
{
cout << "Insert the title of the first song: ";
cin.ignore();
getline (cin, arr_str[i].title);
cout << "Insert the artist name of the first song: ";
cin.ignore();
getline (cin, arr_str[i].artist);
cout << "Insert the genre of the first song: ";
cin.ignore();
getline (cin, arr_str[i].genre);
cout << "Insert the length of the first song: ";
cin >> arr_str[i].len;
}
}
void display(music arr_str[],string pName,float totalLength){
{
string sTitle1, sTitle2, sTitle3,pName;
string sArtist1, sArtist2, sArtist3;
string sGenre1, sGenre2, sGenre3;
float sLength1, sLength2, sLength3;
float playlistAverage;
cout << "Choose your favorite genre" << endl;
cout << " 1 : " << sGenre1 << endl;
cout << " 2 : " << sGenre2 << endl;
cout << " 3 : " << sGenre3 << endl;
cout << " 4 : Exit program " << endl;
int choice = 0;
cout << "Your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout << "Wise!" << endl;
break;
case 2:
cout << "Rad!" << endl;
case 3:
cout << "Tubular!" << endl;
break;
case 4:
cout << "Exiting program..." << endl;
break;
}
playlistAverage = totalLength / 3;
for(int k=0;k<3;k++)
{
cout << "Name of the playlist: " << pName << " ";
cout << k+1 << ".-------------------------------------- ";
cout << " Title: " << arr_str[k].title << " ";
cout << " Artist: " << arr_str[k].artist << " ";
cout << " Genre: " << arr_str[k].genre << " ";
cout << " Length: " << arr_str[k].len << " ";
}
cout << "The playlist named " << pName << " has a total length of " << totalLength << " minutes. ";
cout << "Also, the playlist named " << pName << " has an average length
of " << playlistAverage << " minutes ";
}
}
void savedata(music arr_str){
/*opening file to save the entered playlist*/
ofstream fout("DJlist.txt");
if (fout.is_open())
{
/*adding the contents of the array to the file*/
for(int count = 0; count < sizeof(arr_str); count ++){
fout << arr_str[count].title << " " ;
fout << arr_str[count].artist << " " ;
fout << arr_str[count].genre << " " ;
fout << arr_str[count].len << " " ;
}
/*closing the file*/
fout.close();
}
else cout << "Unable to save to file";
}
int main()
{
music arr_str[10];
string pName;
float totalLength = 0.0;
int newChoice=0, i=0, k=0;
cout << "############################################################ ";
cout << " Welcome to the DJ Dance Playlist Manager ";
cout << "############################################################ ";
while(1){
menu();
cin>>newChoice;
switch(newChoice) {
case 1:
pName = manage_playlist();
break;
case 2:
manage_songs(arr_str,i);
totalLength = totalLength + arr_str[i].len;
break;
case 3: display(arr_str,pName,totalLength);
break;
case 4: savedata(arr_str);
case 5: return 0;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.