Question about reading in data from a file and using getline function

Hi everyone. I am relatively new to C++ and I am trying to create a program that reads data about different songs in from a file and displays the total length of all the songs and the average rating of all of them. Here is an example of the data that I would be reading in:

Just Give Me A Reason|P!nk Featuring Nate Ruess|4:22|4.0
When I Was Your Man|Bruno Mars|3:33|3.5
Thrift Shop|Macklemore & Ryan Lewis Featuring Wanz|3:55|4.5

I think my program is close to being done but for some reason it is not returning the correct length and average rating of all of the songs.

I would really appreciate it if someone could take a look at my code and help me to understand what I am doing wrong.

Thanks!

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

// Structure declaration
struct Song
{
    string title;
    string artist;
    int minutes;
    int seconds;
    double rating;
};

// Function prototype
Song getInfo(ifstream &);

int main()
{
    Song song;
    int index = 0, seconds = 0, totalSeconds = 0, minutes = 0, totalMinutes = 0;
    double totalRating = 0.0, avgRating = 0.0;
    ifstream dataFile;
    ofstream outputFile;
    string filename;

    // Get the file name from the user
    cout << "Enter the name of a file containing songs." << endl;
    cin >> filename;

    // Open the file
    dataFile.open(filename.c_str());

    // Test if file opens
    while (!(dataFile))
    {
        cout << "The file you entered does not exist." << endl;
        cout << "Please enter a valid file name: ";
        cin >> filename;
        dataFile.open(filename.c_str());
    }

    // Get info about songs from file
    while (dataFile)
    {

        song = getInfo(dataFile);
        cout << song.title << song.artist << endl;
        cout << song.minutes << ":" << song.seconds << endl;
        cout << song.rating << endl;

        // Totals numbers
        totalMinutes += song.minutes;
        totalSeconds += song.seconds;
        totalRating += song.rating;

        index++;
    }


    // Puts the total minutes and seconds into one nice format
    seconds = totalSeconds % 60;
    totalSeconds -= seconds;

    minutes = totalSeconds / 60;
    totalMinutes += minutes;

    // Calculates average rating
    avgRating = totalRating / index;

    cout << "Minutes: " << totalMinutes << endl;
    cout << "seconds: " << totalSeconds << endl;
    cout << "average rating: " << avgRating << endl;
    cout << "index: " << index << endl;

    // Open file for output
    outputFile.open("output.txt");



    // Print summary findings to output file
    if (totalSeconds < 10)
    {
        cout << "\n\nThe total length of the song titles in the input file is: "
            << totalMinutes << ":0" << totalSeconds << endl;
    }
    else
    {
        cout << "\n\nThe total length of the song titles in the input file is: "
            << totalMinutes << ":" << totalSeconds << endl;
    }

    cout << "The average rating of the song titles in the input file is: "
        << song.rating << "." << endl;

    // Close the files
    dataFile.close();
    outputFile.close();

    return 0;
}

    // Function that populates Song structure with information
    Song getInfo(ifstream &inFile)
    {
        Song tempSong;              // Temporary structure variable
        string title, artist, rating;

        // Populates structure with song information

            // Read in song title
            getline(inFile, title, '|');
            tempSong.title = title;

            // Read in Artist name
            getline(inFile, artist, '|');
            tempSong.artist = artist;

            // Read in the minutes
            inFile >> tempSong.minutes;

            inFile.get();

            // Read in seconds
            inFile >> tempSong.seconds;

            inFile.get();

            // Read in the rating of the song
            inFile >> tempSong.rating;

        return tempSong;
    }

Last edited on
The problem is that the operator>> expects a space as delimiter not a ':'

inFile >> tempSong.minutes;
will fail. You need to read minutes/seconds as string and convert it (using stringstream) after that
Topic archived. No new replies allowed.