with this code it reads file.txt into a multidimensional array, but when i run it prints the element multiple times and program crashes, can someone help explain why?
enum mediaType {CD, TAPE, MP3};
struct Song
{char title[20];
char artist[20];
mediaType medium;
float cost;
};
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string Song[4][200];
ifstream myfile;
myfile.open("song.txt");
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
cin.get();
return -1;
}
long a = 0;
while(!myfile.eof())
{
for(long b = 0; b < 4; b++)
{
if (b == 3)
{
getline(myfile,Song[a][b],'\n');
break;
}
getline(myfile,Song[a][b],' ');
}
cout << Song[0][0] << ' ';
a++;
}
cin.get();
}
(song.txt)
Armstrong muggles C 4.99
Williams Superman T 1.99
krall S'wonderful C 3.99
Beethoven for_elise M 2.99
ponty watching_birds M 4.10
Armstrong Wild_man_blues C 4.56
Marley work T 3.10
krall love_letters C 3.79
Beethoven Romance_in_F C 2.88
krall cry_me_a_river C 3.69
ponty intuition M 4.20
Armstrong Hear_me_talking_to_ya M 4.12
Mouskouri crie T 2.10
ponty open_mind T 4.40
Marley bad_card M 3.20
Marley real_situation C 3.30
Armstrong muskrat T 4.88
Mouskouri vivants T 2.20
ponty solitude C 4.30