need help!

I have to write a program for this homework below, but I'm stuck.I am fairly new to programming, so please help me to find a simpler way.

You are burning music CD's for a party. You've arranged a list of songs in the order 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 they fit. Write a C++ program to help you do this. The data are on file songs.dat. The time is entered as seconds, but the output converts the seconds to minutes and seconds. After all the data has been read, the application should print a message indicating the time left on the CD. The output should be in the form of a table with columns and headings written on a file.

For example:

Song Song Time Total Time
Number Minutes Seconds Minutes Seconds
------- ------- ------- ------- -------
1 5 10 5 10
2 7 42 12 52
3 4 19 17 11
4 4 33 21 44
5 10 27 32 11
6 8 55 41 6
7 5 0 46 6

There are 33 minutes and 54 seconds of space left on the 80 minute CD.

Note that the output converts the input from seconds to minutes and seconds. Use meaningful variable names, proper indentation, and appropriate comments. Thoroughly test the program using your own data sets.

code below does not work right at all: I am trying to find a much simpler way to do this problem in the first place. when it runs, "it says cant open input file, cancelling."

#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 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;

inData >> totalTime;

songNumber = 1;
while(songNumber <= 14)
{
inData >>current;

if (current > 59)
{
currentMinutes = (current / 60);
currentSeconds = (current % 60);
}

timeSeconds = currentSeconds;
songMinutes = currentMinutes;
seconds = (songMinutes * 60);
usedMinutes = 0;
usedSeconds = 0;

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 - usedTime) % 60;

songNumber++;
}

totalTime = (80* 60);

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;
}

Last edited on
Please use output and code tags to format your "output" and code. This makes it much easier for us to read and help.

One way to make it easier is to use only seconds, rather than seconds and minutes.
Last edited on
I second putting your code in [code][/code] tags.

Regarding the error you're getting, do you actually have songs.dat? If so, is it on your F:\ drive? If so, are you using an online compiler?

The way you're setting up your headings is too complicated. Also, it looks like they might be too long. In general, you want to keep each line at 80 characters or less for console output. You should just do something like this:

1
2
3
    cout << " Song      Song Time       Total Time" << endl;
    cout << "Number  Minutes Seconds  Minutes Seconds" << endl;
    cout << "------  ------- -------  ------- -------" << endl;


When you're outputting data in your loop, use this:

1
2
3
        cout << setw(6) << songNum << setw(9) << songMin
             << setw(7) << songSec << setw(9) << totalMin
             << setw(7) << totalSec << endl;


There is no reason to use fixed << showpoint << setprecision(2) when you're only dealing with integers.
Last edited on
Topic archived. No new replies allowed.