i need to write program so when you enter artist name it outputs information given in following struct, using information in txt file below. i need help getting started in how to start off on how to read txt file into array of these song objects.
(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
My first comment is that your float variable cost should be an array. (After all, each song has a different cost, right?) Here's a short sample I wrote for someone with a similar question. Study it and see if it helps.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string str[30][3];
ifstream myfile("testfile.txt");
int a = 0;
int b = 0;
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfile.eof())
{
getline(myfile,str[a][b],' ');
if(a ==29)
{
a=0;
++b;
getline(myfile,str[a][b],' ');
}
a++;
}
system("pause");
}
testfile.txt
a b c d e f g h i j k l m n o p q r s t u v w x y z aa bb cc dd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
This was written to store a file of columns and rows into a multidimensional array, but the same general principals apply. If you have any problems, just post back with your question.
with the following code it stores the entire file into first element of array, how do i have each string in file into seperate element, also how would i put cost variable into an array.
i modified code and the strings from the txt file goes into the array, but it prints it multiple times and crashes. Can someone tell me why it crashes? might be because of the break?