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

Sounds simple if you write it out. You are given a set amount of seconds. Divide that number by 60 and is that the number of minutes for the song and the remainder the seconds in the song. Now just update your variable that holds the total time you've calculated so far. Assuming the file given will never go above the CD's time capacity so therefore there will no need for a check to see if your time limit has been reach and/or surpassed. Also just priting out the time in minutes and seconds as your read in the file. Also your can format your program's output to the console better using the iomanip left function about all of your cout's outputting your data as such

cout << left;
cout << ... and so on

along with iomanip's setw function spaced properly, your setw() needs a bit tweaking
Here is a little psuedo code to help you

MAX_SECONDS = 480,000

while(not at end of file)
{
 read_in -> song_time_in_seconds
 output: Song Number, Song's Time Minutes = song_time_in_seconds/60, 
         Seconds  = song_time_in_seconds%60
 update total_songs_time variable 
}

//Assuming your file will always be less than or equal to the limit on the CD the next //step is easy

time_remaining_in_seconds = MAX_SECONDS - total_songs_time 

output:
 Time remaining Minutes: (time_remaining_in_seconds / 60), 
Seconds:(time_remaining_in_seconds % 60)

thanks for you help. However, i'm confused about the total_songs_time variable, how do you update it? In my code, the result gives minutes and seconds of every song correctly corresponding to the seconds read from the file. However, the total time is not correct. My program has 2 variables totalMinutes and totalSeconds, i wonder if i really need them.

you can take a look at the output i attach here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.