I have a DJ program, I need to Add the ability to save data to a disk in one or
ID: 3710546 • Letter: I
Question
I have a DJ program, I need to Add the ability to save data to a disk in one or more files. The menu(s) should give the user the option to save or retrieve data. I dont know how to adjust my program to reflect that
#include <iostream>
#include <string>
using namespace std;
struct music
{
string title;
string artist;
string genre;
float len;
};
void menu() {
//cout << "1. create Playlist ";
cout << "2. Enter your Songs ";
cout << "3. Display the Playlist ";
cout << "4. Exit the program. ";
cout << "Enter the number according to your choice: ";
}
string manage_playlist() {
string pName;
cin.ignore();
cout << "Select 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<5; k++, i++)
{
cout << " Enter the title of the song: ";
cin.ignore();
getline(cin, arr_str[i].title);
cout << "Insert the artist name, or Name of the group : ";
cin.ignore();
getline(cin, arr_str[i].artist);
cout << " 1=Reggae, 2=Soca, 3=Disco. Select the genre: ";
cin.ignore();
getline(cin, arr_str[i].genre);
cout << "Insert the length of the song: ";
cin >> arr_str[i].len;
cout << " " << endl;
cout << " " << endl;
}
}
void display(music arr_str[], string pName, float totalLength) {
{
string sTitle1, sTitle2, sTitle3, sTitle4, sTitle5, pName;
string sArtist1, sArtist2, sArtist3, sArtist4, sArtist5;
string sGenre1, sGenre2, sGenre3, sGenre4, sGenre5;
float sLength1, sLength2, sLength3, sLength4, sLength5;
float playlistAverage;
cout << "Choose your genre" << endl;
cout << " 1 : Reggae " << sGenre1 << endl;
cout << " 2 : Soca " << sGenre2 << endl;
cout << " 3 : Disco " << sGenre3 << endl;
cout << " 4 : Exit program " << endl;
int choice;
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 / 5;
for (int k = 0; k<5; 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 << pName << " has a total length of " << totalLength << " minutes. ";
cout << 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 << " Welcome to the Grace Party Playlist ";
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[k].len;
break;
case 3: display(arr_str, pName, totalLength);
break;
case 4: return 0;
}
}
}
Explanation / Answer
ifstream in(filename.c_str()); // for retriving data from file
outstream out(filename.c_str()); //for saving data to file
Actually The question didn't understood... I got what you expect output...
you can use these above mention two lines of code for save and retrive data from file...
It is use as in instead of cin as well as out instead of cout.
in read input from file and out writes data to file
If you still understood, please give me comment... I will help you until you get output....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.