This is the question : you are burning some music CDs for a party. You've arranged a list of songs in the order in which you want to play them. However, you would like to maximize your use of space on the CD, which holds 80 minutes of music. So, you want to figure out the total time for a group of songs and see how well they fit. Write and design a C++ program to help you do this. The data are on the file songs.dat. The time is entered as seconds. For example, if a song takes 7 minutes and 42 seconds to play, the data entered in the data file for that songs would be 462.
After all the data has been read, the application should print a message indicating the time remaining on the CD.
So this is the code i made, however i don't get the result as expetected. The accumulated total time on each line is not correct. I think the reason lies in math logic part, but i can't figure it out which part i'm doing wroing here.
I created my own songs.dat file that looks like this:
4579
325
145
120
280
400
315
580
560
890
169
232
351
116
96
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int songMinutes = 0;
int songSeconds = 0;
int timeSeconds = 0;
int totalMinutes = 0;
int totalSeconds = 0;
int minutesLeft = 0;
int secondsLeft = 0;
int totalTime = 0;
int usedTime = 0;
int current = 0;
int currentMinutes = 0;
int currentSeconds = 0;
int usedMinutes;
int usedSeconds;
int seconds = 0;
int newtotalTime = 0;
int songNumber;
bool ok = true;
ifstream inData;
inData.open("f:\\songs.dat");
if ( !inData )
{
cout << "Can't open the input file. Cancelling." << endl;
cin.get();
return 1;
}
songNumber = 1; //First record
//Create columns and heading
cout << setw(8) << "Song" << setw(28)
<< "Song Time" << setw(28)
<< "Total Time" << setw(25)
<< "Number" << setw(20)
<< "Minutes" << setw(10)
<< "Seconds" << setw(18)
<< "Minutes" << setw(10)
<< "Seconds" << setw(22)
<< "-------" << setw(20) << "-------" << setw(10)
<< "-------" << setw(18) << "-------" << setw(10)
<< "-------" << endl;
/*
cout << fixed << showpoint << setprecision(2) << setw(7)
<< songNumber << setw(18)
<< songMinutes << setw(10)
<< timeSeconds << setw(18)
<< totalMinutes << setw(10)
<< totalSeconds << setw(15)
<< endl;
*/
inData >> totalTime;
// songNumber = 2;
songNumber = 1;
while(songNumber <= 14) //Loop until current complete
{
inData >>current;
// if (currentSeconds > 59)
if (current > 59)
{
currentMinutes = (current / 60);
currentSeconds = (current % 60);
}
timeSeconds = currentSeconds;
songMinutes = currentMinutes;
seconds = (songMinutes * 60);
usedMinutes = 0;
usedSeconds = 0;
/*
if (currentSeconds > 59)
{
currentMinutes = (current / 60);
currentSeconds = (current % 60);
}
*/
totalMinutes = (usedMinutes + currentMinutes);
totalSeconds = (usedSeconds + currentSeconds);
cout << fixed << showpoint << setprecision(2) << setw(7)
<< songNumber << setw(18)
<< songMinutes << setw(10)
<< timeSeconds << setw(18)
<< totalMinutes << setw(10)
<< totalSeconds << setw(15) << endl;
usedTime = (totalMinutes * 60 + totalSeconds);
minutesLeft = (totalTime - usedTime) / 60;
// secondsLeft = (totalTime - minutesLeft) % 60;
secondsLeft = (totalTime - usedTime) % 60;
songNumber++;
}
//Calculates the time on the cd, time used,
//and the time left on the cd
totalTime = (80 * 60);
//Output the minutes and seconds left on the CD
cout << "There are " << minutesLeft << " minutes and "
<< secondsLeft
<< " seconds left of space on the 80 minute CD." << endl;
inData.close();
cin.get();
cin.get();
return 0;
}
This is the result
Song song Time total Time
Number Minutes Seconds Minutes Seconds
------- ------- ------- ------- -------
1 5 25 5 25
2 2 25 2 25
3 2 0 2 0
4 4 40 4 40
5 6 40 6 40
6 5 15 5 15
7 9 40 9 40
8 9 20 9 20
9 14 50 14 50
10 2 49 2 49
11 3 52 3 52
12 5 51 5 51
13 1 56 1 56
14 1 36 1 36