Error Reading file and populating object array

Im trying to read a file in of song data and populate an array of objects, but it will only read in the first line and populate all of the objects with the same data. It also wont recognize the eof, so for now I have it hard coded to read 6 times

Does anyone have any idea what I'm doing wrong?


Here is my current read file
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
void readFile(song songs[], int &size)
{    
    string tempName;
    string tempArtist;
    string tempGenre;
    int tempMin = 0;
    int tempSec = 0;
    
    // opens airport name and location file
    ifstream inFile;    
    inFile.open("songlist.txt");
    
    // checks to see if the file opened correctly    
    if (!inFile)
       cout << "Error opening file\n";
    else
    {
        int i = 0;
        // reads in all the element data
        while (i < 6)//(!inFile.eof())
        {
           cout << "\nsize " << size << endl;
           
           inFile >>  tempName;  // Reads in name
           cout << tempName << endl;
           songs[i].setName(tempName);  // Sets Name
           
           inFile >> skipws >> tempArtist;  // Reads in artist
           cout << tempArtist << endl;
           songs[i].setArtist(tempArtist);  // Sets artist
           
           inFile >> skipws >> tempGenre;  // Reads in genre
           cout << tempGenre << endl;
           songs[i].setGenre(tempGenre);  // Sets genre
           
           inFile >> skipws >> tempMin;  // Reads in minutes
           cout << tempMin << endl;
           songs[i].setMin(tempMin); // Sets minutes
           
           inFile >> skipws >> tempSec;  // Reads in seconds
           cout << tempSec << endl;
           songs[i].setSec(tempSec); // Sets seconds
           
           i++;
           size++;  // counts number of songs read in
        }        
        // closes the file
        inFile.close();       
    } 
};


this is the current output
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
size 0
Turn_the_Page
Bob_Segar
Rock
5
0

size 1
Turn_the_Page
Bob_Segar
Rock
5
0

size 2
Turn_the_Page
Bob_Segar
Rock
5
0

size 3
Turn_the_Page
Bob_Segar
Rock
5
0

size 4
Turn_the_Page
Bob_Segar
Rock
5
0

size 5
Turn_the_Page
Bob_Segar
Rock
5
0
Bob_Segar

num songs 6
Press any key to continue . . .


this is the songlist.txt file
1
2
3
4
5
6
Turn_the_Page Bob_Segar Rock 5:04
Bohemian_Rhapsody Queen Rock 5:55
Miss_America Styx Rock 4:59
The_Wreck_Of_The_Edmund_Fitzgerald Gordon_Lightfoot Ballad 6:30
Lonely_People America Easy 2:29
Bad_Romance Lady_Gaga Pop 4:57
Last edited on
I think the problem is that you don't read the ':' char.
thank you, that fixed it
Last edited on
Topic archived. No new replies allowed.