Create a struct (called AFile) that stores information for an audio file (like a
ID: 3770339 • Letter: C
Question
Create a struct (called AFile) that stores information for an audio file (like an mp3 file). Each struct should have: A string object For The Title A String object For The Artist Name| An Int For Length of The File (In Seconds) An Enumerated Type For Mp3 (For .Mp3 Files) or Wav (For .Wav Files) Your database of audio files will consist of a dynamic array of these structs. This is essentially your database. Your program must: allow entry of a single affile into the database. Allow the user to print all afile information. Allow editing of an afile entry. You must also handle cases where the size of your dynamic array is increased. For example. If your array starts off as an array of length 10, before you can get input into element 11, you need to create a new larger array of say... Length 20, copy the elements over and then get input into the new array. Also... Include options to save and retrieve your database of audio files to/from a text file, upon retrieval, load your data into a dynamic array of aflles. You will probably have to read through the file once to determine the number of entries in the file and then a second time to actually read the data in...Explanation / Answer
Answer:
Program code to copy:
// SongsStructureList.cpp :
//Header files
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
enum type{MP3, WAV};
//AFile structure
struct AFile
{
string artistName;
string songTitle;
int length;
type value;
};
//prototypes
void addSong(AFile *s, int &count);
void deleteSong(AFile *s, int &count);
void editFile(AFile *s, int count);
void printSongsData(AFile *s, int count);
void sortData(AFile *s, int count);
void copyData(AFile *s, AFile *newS, int &count);
//main function
int main()
{
int MAX=300;
//array of structures
AFile *s = new AFile [MAX];
AFile *newS = new AFile;
string name;
string typo;
string ch;
//increment variable and choice variables
int count=0;
int choice;
//file object
fstream infile("topsong.txt");
//if the file exist proceed to read the
//data from the file
if(infile)
{
//loop till end of the file
while(!infile.eof())
{
infile>> s[count].artistName;
infile>> s[count].songTitle;
infile>> s[count].length;
infile>>typo;
if(typo.compare("MP3")==0)
s[count].value=MP3;
else
s[count].value=WAV;
count++;
}
}
//else print a error message
else
{
cout<<"Sorry! File not found."<<endl;
}
//close the file
infile.close();
if(count >= MAX)
copyData(s, newS, count);
//sort the elements in the array
sortData(s, count);
//menu
do
{
cout<<" Songs List "<<endl;
cout<<"Please select from the following menu choices: "<<endl;
cout<<"1. Add a song"<<endl;
cout<<"2. Delete a song"<<endl;
cout<<"3. Print"<<endl;
cout<<"4. Edit"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Enter choice: ";
cin>>choice;
cout<<endl;
//depending on the user choice
//the values are printed
switch(choice)
{
case 1:
addSong(s, count);
break;
case 2:
deleteSong(s, count);
break;
case 3:
printSongsData(s, count);
break;
case 4:
editFile(s, count);
sortData(s, count);
break;
case 5:
exit(0);
break;
default:
cout<<"Enter your choice properly."<<endl;
}
}while(true);
system("pause");
return 0;
}
//definition of the addSong method
//adds a song with the respective details
void addSong(AFile *s, int &count)
{
string typo;
cout<<"Enter the name of the artist(Fname_Lname): ";
cin>>s[count].artistName;
cout<<"Enter the title of the song: ";
cin>>s[count].songTitle;
cout<<"Enter the length of the song: ";
cin>>s[count].length;
cout<<"Enter the song audio format(MP3/WAV): ";
cin>>typo;
if (typo.compare("MP3")==0)
s[count].value = MP3;
else
s[count].value = WAV;
count++;
sortData(s, count);
}
//definition of the deleteSong method
//delets a song as per the user request
void deleteSong(AFile *s, int &count)
{
AFile song;
string tsong;
bool flag;
cout<<"Enter the song title: ";
cin>>tsong;
for(int i=0;i<count;i++)
{
if(tsong.compare(s[i].songTitle)==0)
{
flag=true;
song=s[i];
s[i]=s[i+1];
}
}
if(flag)
{
count--;
}
}
//definition of the printSongsData method
//prints the details of the list of songs
void printSongsData(AFile *s, int count)
{
cout<<setw(-5)<<"Artist Name "<<setw(20)<<"Song Title "<<setw(20)<<" Length "<<setw(10)<<" Format "<<endl;
cout<<" =============================================================================== ";
for(int i=0;i<count;i++)
{
cout<<s[i].artistName<<" "<<setw(20)<<s[i].songTitle<<setw(15)<<s[i].length<<setw(10);
if(s[i].value == MP3)
cout<<"MP3"<<endl;
else
cout<<"WAV"<<endl;
}
}
//sortData method will sort the list of songs in the list
void sortData(AFile *s, int count)
{
AFile temp;
for (int i=0; i <count; i++)
{
for(int j=0;j<count;j++)
{
if((s[i].artistName.compare(s[j].artistName))<0)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
//copyData method will copy the data from the old list to the list
//if the size of array is outofbounds
void copyData(AFile *s, AFile *newS, int &count)
{
int size = 2*count;
newS = new AFile[size];
for(int i=0;i<count;i++)
{
newS[i]=s[i];
}
s = new AFile[size];
s=newS;
count = size;
}
//editFile method will edits the data of the old list
void editFile(AFile *s, int count)
{
string name;
int option;
cout<<"Enter the title so that the record data can be modified."<<endl;
cout<<"Enter the title of song(Song_name): ";
cin>>name;
for(int i=0;i<count;i++)
{
if(name.compare(s[i].songTitle)==0)
{
cout<<"1. Artist Name"<<endl;
cout<<"2. Song Title"<<endl;
cout<<"3. Song length"<<endl;
cout<<"4. Song format"<<endl;
cout<<">> ";
cin>>option;
switch(option)
{
case 1:
cout<<"Enter the new artist name: ";
cin>>name;
s[i].artistName=name;
break;
case 2:
cout<<"Enter the new title name: ";
cin>>name;
s[i].songTitle=name;
break;
case 3:
cout<<"Enter the new length: ";
cin>>option;
s[i].length = option;
break;
case 4:
cout<<"Enter the new audio format(MP3/WAV): ";
cin>>name;
if(name.compare("MP3"))
s[count].value=MP3;
else
s[count].value=WAV;
break;
}
break;
}
}
}
Sample Text file: topsong.txt
Katy_Perry Dark_Horse 240 MP3
Susan_Boyle Tik_Tok 201 WAV
Susan_Boyle I_Dreamed_a_Dream 109 WAV
Lady_Gaga Applause 250 MP3
AR_Rehman Jai_Ho 300 WAV
Jang_Geun_Suk What_should_I_Do 320 WAV
Geun_Suk_Meon Let_Me_Cry 280 MP3
Gong_Won_Suk Magic_Drag 320 MP3
Milly_Kate Nature_Girl 200 WAV
Jang_Youn_Me Take_Me 314 MP3
Sample Output:
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 3
Artist Name Song Title Length Format
===============================================================================
AR_Rehman Jai_Ho 300 WAV
Geun_Suk_Meon Let_Me_Cry 280 MP3
Gong_Won_Suk Magic_Drag 320 MP3
Jang_Geun_Suk What_should_I_Do 320 WAV
Jang_Youn_Me Take_Me 314 MP3
Katy_Perry Dark_Horse 240 MP3
Lady_Gaga Applause 250 MP3
Milly_Kate Nature_Girl 200 WAV
Susan_Boyle I_Dreamed_a_Dream 109 WAV
Susan_Boyle Tik_Tok 201 WAV
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 4
Enter the title so that the record data can be modified.
Enter the title of song(Song_name): Tik_Tok
1. Artist Name
2. Song Title
3. Song length
4. Song format
>> 1
Enter the new artist name: Boyle_Susan
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 3
Artist Name Song Title Length Format
===============================================================================
AR_Rehman Jai_Ho 300 WAV
Boyle_Susan Tik_Tok 201 WAV
Geun_Suk_Meon Let_Me_Cry 280 MP3
Gong_Won_Suk Magic_Drag 320 MP3
Jang_Geun_Suk What_should_I_Do 320 WAV
Jang_Youn_Me Take_Me 314 MP3
Katy_Perry Dark_Horse 240 MP3
Lady_Gaga Applause 250 MP3
Milly_Kate Nature_Girl 200 WAV
Susan_Boyle I_Dreamed_a_Dream 109 WAV
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 1
Enter the name of the artist(Fname_Lname): Park_Shiny
Enter the title of the song: Love_Will_Heal
Enter the length of the song: 280
Enter the song audio format(MP3/WAV): MP3
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 3
Artist Name Song Title Length Format
===============================================================================
AR_Rehman Jai_Ho 300 WAV
Boyle_Susan Tik_Tok 201 WAV
Geun_Suk_Meon Let_Me_Cry 280 MP3
Gong_Won_Suk Magic_Drag 320 MP3
Jang_Geun_Suk What_should_I_Do 320 WAV
Jang_Youn_Me Take_Me 314 MP3
Katy_Perry Dark_Horse 240 MP3
Lady_Gaga Applause 250 MP3
Milly_Kate Nature_Girl 200 WAV
Park_Shiny Love_Will_Heal 280 MP3
Susan_Boyle I_Dreamed_a_Dream 109 WAV
Songs List
Please select from the following menu choices:
1. Add a song
2. Delete a song
3. Print
4. Edit
5. Exit
Enter choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.