Hello, I was wondering if someone could help debug my C++ code. The question is.
ID: 3639377 • Letter: H
Question
Hello, I was wondering if someone could help debug my C++ code. The question is...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. To do so, you want to figure out the total time for a group of songs and see how well they fit. Write a design and a c++ program to help you accomplish this task. The data are on file songs.dat. The time is entered as seconds. For exmple, if a song takes 7 minutes and 42 seconds to play, the data entered for that song would be 462. After all the data has been read, the application should print a message indicating time remaining on the CD. The output should be in the form of a table with columns and headings written on a file.
This is what i have so far...
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int songNumber, songMinutes, songSeconds;
int totalMinutes = 0;
int totalSeconds = 0;
int minutesLeft = 0;
int secondsLeft = 0;
int totalTime = 0;
int usedTime = 0;
int currentSong = 0;
int currentSongMinutes = 0;
int currentSongSeconds = 0;
int seconds;
int usedMinutes = 0;
int usedSeconds = 0;
ifstream inData;
inData.open("songs.dat");
if (!inData)
{
cout << "Can't open the file. Try again." << endl;
return 1;
}
else
{
cout << setw(8) << "Song " << setw(28) << "Song Song" << setw(28) << "Total Total" << 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(inData)
{
inData >> currentSong;
currentSongMinutes = (currentSong / 60);
currentSongSeconds = (currentSong % 60);
songSeconds = currentSongSeconds;
songMinutes = currentSongMinutes;
seconds = (songMinutes * 60);
usedMinutes = 0;
usedSeconds = 0;
totalMinutes = (usedMinutes + currentSongMinutes);
totalSeconds = (usedSeconds + currentSongSeconds);
cout << fixed << showpoint << setprecision(2) << setw(7) << songNumber << setw(18) << songMinutes << setw(10)
<< songSeconds << 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 of space left on the 80-minute CD." << endl << endl;
inData.close();
return 0;
}
}
It would be greatly appreciated if someone could fix my code. It needs to add up the total time and tell you how much time you have left.
Thanks in advance!
Explanation / Answer
Ok, I think this should be working the way you wanted it to. The repeat value comes from extra empty lines at the end of the dat file. #include #include #include using namespace std; int main() { int songNumber, songMinutes, songSeconds; int totalMinutes = 0; int totalSeconds = 0; int minutesLeft = 0; int secondsLeft = 0; int totalTime = 0; int usedTime = 0; int currentSong = 0; int currentSongMinutes = 0; int currentSongSeconds = 0; int seconds; int usedMinutes = 0; int usedSeconds = 0; totalTime = (80 * 60); ifstream inData; inData.open("songs.dat"); if (!inData.is_open()) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.