This question asks for 1 answer that does the things stated below, thus it is no
ID: 646014 • Letter: T
Question
This question asks for 1 answer that does the things stated below, thus it is not able to be split into multiple posts. We just began learning about arrays which explains the multiple requirements for this question.
This question asks for 1 answer that does the things stated below, thus it is not able to be split into multiple posts. We just began learning about arrays which explains the multiple requirements for this question. Some Further Information Given: Example of forth entry in CD Database: * * * *TIP: You must have some sort of variable that keeps track of the number of CDs entered. ----This will start out at 0 and increment by 1 each time a CD is entered---. The best way to approach this program is to do everything except the file input/output FIRST. Once that is working, then add in the options to store and retrieve the data. Recall use of ifstream and ofstream. AND I Recommend storing the data into a text file delimited by new lines. For Example: Fernando ABBA 120 Crazy Train Ozzy Osborne 132Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void printMenu(){
cout << "1. Add MP3" << endl;
cout << "2. Print out MP3 list" << endl;
cout << "3. Modify MP3 entry" << endl;
cout << "4. Save List" << endl;
cout << "5. Retrieve List" << endl;
cout << "6. Quit Program" << endl;
}
void printList(string title[], string artist[], int play_length[], int count){
for(int i = 0; i < count; ++i){
cout << "Track " << (i + 1) << ": " << endl;
cout << "Title: " << title[i] << endl;
cout << "Artist: " << artist[i] << endl;
cout << "Length: " << play_length[i] << endl;
}
}
int getIndex(string title[], string t, int count){
for(int i = 0; i < count; ++i){
if(title[i] == t){
return i;
}
}
return -1;
}
void addEntry(string title[], string artist[], int play_length[], int index){
cout << "Enter Title of the Track: ";
getline(cin, title[index]);
cout << "Enter Name of the artist: ";
getline(cin, artist[index]);
cout << "Enter play length of the track: ";
cin >> play_length[index];
string temp;
getline(cin, temp);
}
void saveList(string title[], string artist[], int play_length[], int count){
ofstream out;
out.open("mp3list.txt");
if(out.is_open()){
for(int i = 0; i < count; ++i){
out << i + 1 << endl;
out << title[i] << endl;
out << artist[i] << endl;
out << play_length[i] << endl;
}
out.close();
}
else{
cout << "Can not open mp3list.txt" << endl;
}
}
void retrieveList(string title[], string artist[], int play_length[], int &count){
ifstream in;
in.open("mp3list.txt");
int val;
count = 0;
string temp;
if(in.is_open()){
while(in >> val){
getline(in, temp);
getline(in, title[count]);
getline(in, artist[count]);
in >> play_length[count];
getline(in, temp);
count++;
}
in.close();
}
else{
cout << "Can not open mp3list.txt" << endl;
}
}
int main(){
string title[50], temp;
string artist[50];
int play_length[50], option, count = 0, ind;
while(true){
printMenu();
cout << "Choose an option: ";
cin >> option;
getline(cin, temp); // this extra cin after every cin is to skip past the character
switch(option){
case 1:
addEntry(title, artist, play_length, count);
count++;
break;
case 2:
printList(title, artist, play_length, count);
break;
case 3:
cout << "Enter title of the Track to modify: ";
getline(cin, temp);
ind = getIndex(title, temp, count);
if(ind == -1){
cout << temp << " not found" << endl;
}
else{
addEntry(title, artist, play_length, ind);
}
break;
case 4:
saveList(title, artist, play_length, count);
break;
case 5:
retrieveList(title, artist, play_length, count);
break;
case 6:
return 0;
break;
default:
cout << "Please choose a valid option!" << endl;
}
cout << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.