string array of structs from a data file

I am having a problem dividing a string so that I can create an array with structs. I can get the data to divide at the "|", but how do I get the array to accept the struct I created?

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

#define inFileMusic "tophits.txt" //external top 10 hots file


struct music
{
string songTitle;
string artistName;
};
const int MAX_WEEK = 10;
string weeklyHits[MAX_WEEK];
string numberOnes (string);


int main()
{
ifstream hitIn;

hitIn.open(inFileMusic);
if (hitIn.fail())
{
cerr<<"ERROR: cannot open "<<inFileMusic<<"for input."<<endl;
}

for (int y=1;y<7;y++)
{ cout<<"WEEK "<<y<<endl;

for (int i=0;i<MAX_WEEK;i++)
{
getline(hitIn,weeklyHits[i],'|');
}

for (int m=1;m<MAX_WEEK;m++)
{
cout<<"Number "<<m<<" Hit "<<weeklyHits[m]<<endl;
}
}
hitIn.close();

return 0;


the test file looks like this:

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

Whenever I add music weeklyHits[MAX_WEEK}; I get this error

error: no matching function for call to ‘getline(std::ifstream&, music&, char)’

note: candidates are:

note: ‘music’ is not derived from ‘std::basic_string<_CharT, _Traits, _Alloc>’

note: ‘music’ is not derived from ‘std::basic_string<_CharT, _Traits, _Alloc>’
----
Your code is missing the std::string header
#include <string>
I added that and it does not make a difference.
The problem being is that you must read to the strings int he music structure what you are trying to call is operator << music basically without it being overlaoded.

Try

1
2
3
4
5
for( int i = 0; i < weeks; ++i )
{
    getline( in , weeklyHits[i].songTitle , '|' );
    getline( in , weeklyHits[i].artistname ); //get rest of that line or use '\n' as delim
}


Thank you. I had tried something like that before and it didn't work. Perhaps I cleaned up the code since then.
Topic archived. No new replies allowed.