Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing this program, below are the directions input.dat information

ID: 3917841 • Letter: I

Question

I need help writing this program, below are the directions

input.dat information

Be sure that you downloaded the input file input.dat In the global space, define a struct called Album The members of the struct shall include: - artist - title year - numTracks Your program shall open the input.dat file and read the contents into memory - The very first value in the file always states how many albums are in the file - This is how you'll know how large your array should be You could dynamically allocate the array After reading the albums from the file, the program will prompt the user if they want to add an album - Only one album will be added whenever the program is run The albums will then be sorted by artist Then the albums will be written back to input.dat

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<sstream>

using namespace std;

struct Album{
    string artist;
    string title;
    int year;
    int numTracks;
};

int main(){

   Album data[100];
   ifstream fin("input.dat");
   if (!fin){
       cout << "Error opening file ";
       return 0;
   }
   int n;
   string line;
   getline(fin,line);
   n = atoi(line.c_str());
  
   for (int i = 0; i<n; i++){
       getline(fin,data[i].artist);
       getline(fin,data[i].title);
       //cout << data[i].title << " " << data[i].artist<< endl;
       string line;
       getline(fin,line);
       data[i].year = atoi(line.c_str());
       getline(fin,line);
       data[i].numTracks = atoi(line.c_str());
   }
   cout << "Want to add an album:(y/n) :";
   //string line;
   getline(cin,line);
   if (line[0] == 'y'){
       cout << "Enter artist:";
       getline(cin,data[n].artist);
       cout << "Enter title:";
       getline(cin,data[n].title);
       cout << "Enter year:";
       string line;
       getline(cin,line);
       data[n].year = atoi(line.c_str());
       cout << "Enter number of tracks:";
       getline(cin,line);
       data[n].numTracks = atoi(line.c_str());    
       n++;
   }
   /*
   for (int i = 0; i<n; i++){
      cout << data[i].artist << endl;
      cout << data[i].title << endl;
      cout << data[i].year << endl;
      cout << data[i].numTracks << endl;
   }
   */
   for (int i = 0; i<n; i++){
       for (int j = 0; j<n-1-i; j++){
           if (data[j].artist > data[j+1].artist){
               string tempa = data[j].artist;
               string tempt = data[j].title;
               int tempy = data[j].year;
               int tempn = data[j].numTracks;
               data[j].artist = data[j+1].artist;
               data[j].title = data[j+1].title;
               data[j].year = data[j+1].year;
               data[j].numTracks = data[j+1].numTracks;
               data[j+1].artist = tempa;
               data[j+1].title = tempt;
               data[j+1].year = tempy;
               data[j+1].numTracks = tempn;         
           }         
       }
   }
  
   ofstream fout("input.dat");
   fout << n << endl;
   for (int i = 0; i<n; i++){
      fout << data[i].artist << endl;
      fout << data[i].title << endl;
      fout << data[i].year << endl;
      fout << data[i].numTracks << endl;
   }
  
}