My program is sloppy - help intro'ing new functions

Hi, I am taking my very first C++ course and I may have jumped ahead too far. I have this program created that takes song titles and there play times (in the form of HH:MM:SS) from the user and outputs them with a list that shows: The song titles - their play times - and at the bottom, the total time.

My problem is that only the main function is used. Redundant, yes? I need to call different functions in main to make it run smoother:

A function to normalize a time by converting any portions of the seconds greater than 60 into minutes and any portions of the minutes greater than 60 into hours.
This function should be take three parameters, one each for the hours, minuts, and seconds of the time to be examined and, if necessary, changed as per the above description.

A function to add one time (hours, minutes, seconds) to another. The resulting combined time should be normalized.
The function should be named add and should take 6 parameters. The first three are a time (hours, minutes, seconds) and are input only. The second three are the other time to which the first one is being added.

A function to print times in the form H:MM:SS where H is one or more digits long, MM is 2 or more digits long (i.e., 8 minutes should be printed as "08") and SS is 2 or more digits long.
The function should be named printTime and should take a time (three integers: hours minutes, and seconds) as parameters. It should print to cout.

A function to convert an integer in the range 0..99 into a string containing exactly two characters: "00", "01", ..., "10", "11", ... , or "99".
The function should be named twoDigits and should take a single integer parameter and return a string.

Here is my code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;



int main (int argc, char** argv)
{
  int totalHrs = 0;
  int totalMins = 0;
  int totalSecs = 0;

  int hrs;
  while (cin >> hrs)
    {
      // Read a song
      int min, sec;
      string title;
      cin >> min >> sec >> ws;
      getline (cin, title);

      // Normalize the time and print the song
      int m = sec / 60;
      sec = sec % 60;
      min += m;
      int h = min / 60;
      hrs += h;
      min = min % 60;

      // Pad the title to 40 characters
      title = title + string(40, ' ');
      title = title.substr(0, 40);
      
      cout << title;
      cout << hrs << ':';
      string minStr;
      int k = min / 10;
      char c = '0' + k; 
      minStr += c;
      k = min % 10;
      c = '0' + k; 
      minStr += c; 
      cout << minStr;
      cout << ':';
      string secStr;
      k = sec / 10;
      c = '0' + k; 
      secStr += c; 
      k = sec % 10;
      c = '0' + k; 
      secStr += c; 
      cout << secStr << endl;
      
      // Add this time to the sum. 
      // Keep the sum normalized
      //   (useful for debugging purposes)
      totalSecs += sec;
      totalMins += min;
      totalHrs += hrs;
      m = totalSecs / 60;
      totalSecs = totalSecs % 60;
      totalMins += m;
      h = totalMins / 60;
      totalHrs += h;
      totalMins = totalMins % 60;
    }
  // Done with all the songs. Print the total time.
  cout << "Total: ";
  
  cout << totalHrs << ':';
  if (totalMins < 10)
    cout << '0';
  cout << totalMins << ':';
  cout << setfill('0') << setw(2) << totalSecs;
  cout << endl;

  return 0;
}

Thanks for any and all help
Last edited on
use code tags please
I feel I am getting closer to what I need (refer to first post). Although when I input data "0 2 94 On The Run" and " 0 0 127 Eclipse", the program does not "roll over" the seconds to minutes nor minutes to hours (i.e. 0 2 94 should read 0 03 34). Thoughts?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;



int main (int argc, char** argv)
{
  int totalHrs = 0;
  int totalMins = 0;
  int totalSecs = 0;

  int hrs;
  while (cin >> hrs)
    {
      // Read a song
      int min, sec;
      string title;
      cin >> min >> sec >> ws;
      getline (cin, title);

      // Normalize the time and print the song
     sec = sec % 60;
     min = min % 60;
     hrs = min / 60;


      // Pad the title to 40 characters
      title = title + string(40, ' ');
      title = title.substr(0, 40);

     cout << title;
      cout << hrs << ':';
      string minStr;
      int k = min / 10;
      char c = '0' + k;
      minStr += c;
      k = min % 10;
      c = '0' + k;
      minStr += c;
      cout << minStr;
      cout << ':';
      string secStr;
      k = sec / 10;
      c = '0' + k;
      secStr += c;
      k = sec % 10;
      c = '0' + k;
      secStr += c;
      cout << secStr << endl;

      // Add this time to the sum.
      // Keep the sum normalized
      //   (useful for debugging purposes)
      totalSecs += sec;
      totalMins += min;
      totalHrs += hrs;
      min = totalSecs % 60;
      totalSecs = totalSecs %60;
      hrs = totalMins /60;


    }
  // Done with all the songs. Print the total time.
  cout << "Total: ";
  cout << setfill('0') << totalHrs << ':' << setw(2) << totalMins << ':' << setw(2) << totalSecs << endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.