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.

Image for This question asks for 1 answer that does the things stated below, thus it is not able to be split into multipImage for This question asks for 1 answer that does the things stated below, thus it is not able to be split into multip

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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 \n 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;
}

Add a comment
Know the answer?
Add Answer to:
This question asks for 1 answer that does the things stated below, thus it is not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Read the Article posted below, then answer the following questions: Mergers & acquisitions are a major...

    Read the Article posted below, then answer the following questions: Mergers & acquisitions are a major form of corporate diversification strategy, identify and discuss the top three reasons why most (50-60%) of acquisitions fail to create shareholder value. What are the five major components of “CEMEX Way” and why has this approach been so successful in post-acquisition integration? In your opinion, what can other companies learn from the “CEMEX Way” as a benchmark for acquisition management? Article: CEMEX: Globalization "The...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • Comprehensive Income Tax Course: Module 1 4. Randy turned 16 last year and had his first...

    Comprehensive Income Tax Course: Module 1 4. Randy turned 16 last year and had his first summer job. Even though his parents are claiming him as a dependent he wants to file a return in order to get his refund. He receives his W-2 and decides he can do his own return using form 1040-EZ. Which of the following information is not found on a Form W-2? a) The taxpayer’s Social Security number b) The taxpayer’s wages, tips and other...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT