# Thread: New to programming, need help with totaling integers

1. ## New to programming, need help with totaling integers

I am writing a program that will take in data from a file called "songs.dat" and uses that data to output the song length in total seconds (the integers on "song.dat"), the song length in minutes and seconds, and the total minutes and seconds of all songs up until that point. My problem is getting the total length of all of the songs to work, at the moment it is just saying that the total of all of the songs is whatever the length in minutes and seconds the current song is. The function "int totalTime (int time)" is supposed to be calculating the total time in seconds (ie if song.dat reads 60 60 120, the total time in seconds is 240) and the functions totalTime_min and totalTime_sec are supposed to be taking the total value and calculating minutes and seconds out of that value, but something in there is not working the way that it should be. I have toyed around with it for an hour and am hoping that someone here can help me out.

sample output:

http://imageshack.us/a/img28/3487/sc...7oct131807.jpg

Code:
```#include <iostream>
#include <fstream>      // file I/O
#include <cstdlib>      // exit
#include <iomanip>      // setw
using namespace std;

void output (int time);

int songTime_min (int time);

int songTime_sec (int time);

int totalTime (int time);

int totalTime_min (int time);

int totalTime_min (int time);

void open_files (ifstream& filein);

int main ()
{

ifstream file_stream;
int num;

open_files (file_stream);

while (file_stream >> num)
{
output (num);
}

}

int songTime_min (int time)
{

return (time /60);

}

int songTime_sec (int time)
{
return (time % 60);

}

int totalTime (int time)
{

int total = 0;

total = total += time;

return (total);

}

int totalTime_min (int total)
{

return (total / 60);

}

int totalTime_sec (int time)
{

int total = 0;

total = total += time;

return (total % 60);

}

{

cout << endl << endl;
cout << "Song            Song  Time           Total  Time\n";
cout << "length        Minute  Seconds      Minutes  Seconds\n";
cout << "---------------------------------------------------\n";

}

void output (int time)
{

cout.setf(ios::left);

cout << setw (5) << time;

cout.unsetf(ios::left);

cout << setw(13) << songTime_min (time);

cout << setw(7) << songTime_sec (time);

cout << setw(15) << totalTime_min (time);
cout << setw(7) << totalTime_sec (time) << endl;

}

void open_files (ifstream& filein)
{

filein.open ("songs.dat");

if (filein.fail())
{
cout << "File fail to open \n";
exit(1);
}
}```

2. Your total keeps going out of scope.

You need a total which preserves its value from one call to output to the next.