Hello, this is for an intro C++ class. We are using Visual Studio 2013--- file-n
ID: 646010 • Letter: H
Question
Hello, this is for an intro C++ class. We are using Visual Studio 2013--- file-newproject- Win32ConsoleApplication- and writing code on the source files that we compile and then start without debugging which our professor calls printing to screen---- this s where the menu below should display. Please go easy on a newbie/Any help s appreciated, Thanks. Write a code that creates a database of MP3 audio file information. (Create a useable user interface that allows the user to): A) Input data into each file entry B) Modify a file entry C) Print out the entire database of MP3 files D)You must also have options to save and retrieve the database from a text file. So a menu MIGHT look like this: 1. Add MP3 2. Print out MP3 list 3. Modify MP3 entry 4. Save List 5. Retrieve List 6. Quit programExplanation / 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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.