C++ Write the declarations for an array of structs that can be used to store all
ID: 3538601 • Letter: C
Question
C++
Write the declarations for an array of structs that can be used to store all the top 10 hits for every week of the year. The data for the first six weeks of 2012 is stored in a text file (TopHits.txt) that is supplied to you. Each line in the file contains the song title followed by a | followed by the artist. The first 10 records are the hits for the first week (from 1 to 10), the next 10 records are the hits for the next week and so on. Write functions to:
1. Load the array of structs with data from the file.
2. Display the top hit for every week of the year.
You will be supplied with an input file, TopHits.txt (copy this list and save in doc as TopHits.txt)
Sexy & I Know It | LMFAO
We Found Love | Rihanna Featuring Calvin Harris
Good Feeling |Flo Rida
Set Fire To The Rain | Adele
Party Rock Anthem | LMFAO Featuring Lauren Bennett & GoonRock
Someone Like You | Adele
It Will Rain | Bruno Mars
The One That Got Away | Katy Perry
Pumped Up Kicks | Foster The People
Young, Wild & Free | Snoop Dogg & Wiz Khalifa Featuring Bruno Mars
Set Fire To The Rain | Adele
Good Feeling | Flo Rida
Sexy & I Know It | LMFAO
We Found Love | Rihanna Featuring Calvin Harris
Someone Like You | Adele
I Won't Give Up Jason | Mraz
Domino | Jessie J
It Will Rain | Bruno Mars
Young, Wild & Free | Snoop Dogg & Wiz Khalifa Featuring Bruno Mars
Paradise | Coldplay
Set Fire To The Rain | Adele
Good Feeling | Flo Rida
Sexy & I Know It | LMFAO
We Found Love | Rihanna Featuring Calvin Harris
Turn Me On | David Guetta Featuring Nicki Minaj
Domino | Jessie J
Someone Like You | Adele
It Will Rain | Bruno Mars
Young, Wild & Free | Snoop Dogg & Wiz Khalifa Featuring Bruno Mars
Rack City | Tyga
Set Fire To The Rain | Adele
Turn Me On | David Guetta Featuring Nicki Minaj
Good Feeling | Flo Rida
What Doesn't Kill You (Stronger) | Kelly Clarkson
We Found Love | Rihanna Featuring Calvin Harris
Sexy & I Know It | LMFAO
Domino | Jessie J
Somebody That I Used To Know | Gotye Featuring Kimbra
Titanium | David Guetta Featuring Sia
Rack City | Tyga
What Doesn't Kill You (Stronger) | Kelly Clarkson
Set Fire To The Rain | Adele
Turn Me On | David Guetta Featuring Nicki Minaj
Good Feeling | Flo Rida
Somebody That I Used To Know | Gotye Featuring Kimbra
We Found Love | Rihanna Featuring Calvin Harris
Domino | Jessie J
Sexy & I Know It | LMFAO
Titanium | David Guetta Featuring Sia
Rack City | Tyga
What Doesn't Kill You (Stronger) | Kelly Clarkson
Set Fire To The Rain | Adele
Give Me All Your Luvin' | Madonna Featuring Nicki Minaj & M.I.A.
Turn Me On | David Guetta Featuring Nicki Minaj
Somebody That I Used To Know | Gotye Featuring Kimbra
Good Feeling | Flo Rida
Sexy & I Know It | LMFAO
Titanium | David Guetta Featuring Sia
We Found Love | Rihanna Featuring Calvin Harris
Rack City | Tyga
__________________________________________________________________________
For this program what are the....
(Repeat this section for each of your functions). Provide the function prototype here.
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct SongRecord
{
string title;
string artist;
};
int readFile(SongRecord array[])
{
ifstream in("TopHits.txt");
if(!in)
{
cout<<"input file doesnot exits"<<endl;
return 0;
}
int i=0;
string line;
while(!in.eof())
{
getline(in,line);
array[i].title=line.substr(0,line.find_first_of('|')-0);
array[i].artist=line.substr(line.find_first_of('|')+1);
i++;
}
return i-1;
}
void printRecord(SongRecord array[],int i)
{
int j=0;
int week=1;
int hit=1;
while(j<=i)
{
if(j%10==0)
{
cout<<"----------------Week :"<<week++<<"-----------------"<<endl<<endl;
hit=1;
}
cout<<"-------------Hit: "<<hit++<<"------------------"<<endl;
cout<<"title: "<<array[j].title<<endl;
cout<<"artist: "<<array[j].artist<<endl;
cout<<endl;
j++;
}
}
int main()
{
SongRecord array[1000];
int i=readFile(array);
if(i>0)
printRecord(array,i);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.