I'm not even sure I asked the question correctly. This is for an assignment, but I don't want help with the whole thing, at least not yet. It's our longest program yet and I want to try and figure it out. I'm just stuck about this.
Info about the assignment: We have to put songs on a CD. Each song needs to be between 60-600 seconds, inclusive. We then convert the seconds to minutes and seconds. After each song is read, we need to display the song number, the song time, the total time on the CD, and at the end, the amount of minutes and seconds left on the CD. Any song not in rage is not shown on the screen, but when writing to an output file, an invalid error shows.
I was wondering, how would I use an array to access the contents of a file?
She wants the file opened using its own function, so the file's open already. There are 11 numbers in the file, so 11 elements in the array, all initialized.
This is what I have right now, relative to my question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
void readData(int song[], int size)
{
int songNumber = 1; //counter variable to step through each song
int songSeconds; // original seconds of each song (310)
int songMin; // minutes of each song (5:)
int songSec; // seconds of each song (:10)
int totalSeconds; // total original seconds after each song
int totalMin; //total song minutes after each song
int totalSec; //total song seconds after each song
while (inputFile)
{
//read each song's seconds from the file
inputFile >> songSeconds;
//validate data
if(songSeconds >= 60 && songSeconds <= 600)
{
// convert original seconds to minutes and seconds after each song
songMin = songSeconds / 60;
songSec = songSeconds % 60;
//updating total original seconds after each song
totalSeconds += songSeconds;
//convert total original seconds to total minutes and seconds after each song
totalMin = totalSeconds / 60;
totalSec = totalSeconds % 60;
cout << "\n" << songNumber << setw(10) << " " << songMin << setw(10) <<songSec<<setw(18)<<totalMin<<setw(10)<<totalSec;
outputFile << "\n" << songNumber << setw(10) << " " << songMin << setw(10)<<songSec<<setw(18)<<totalMin<<setw(10)<<totalSec;
}
}
|
I may go ahead and make all of my variables global variables. I'm not sure which is better.
Am I using the array correctly? It feels like I'm missing something. It doesn't seem like the array is accessing anything.
Is the way I'm accessing the file correct (and writing to the output file)?