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.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
usingnamespace 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;
}