Function calling issues

I am supposed to develop a program that takes a song title and its playtime from a user in the form of H:MM:SS Song Title. It should print right below it: Song Title Playtime (except playtime is rolled over [i.e 0 0 127 would become 0 02 07]). I will post a before and after code. The before functions as it should, just looks sloppy. The after code is my attempt at making it look better. I failed lol. I could use some help ASAP please. Thanks.
Before 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;
}


After 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
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>

using namespace std;

int main (void)
{
    int totalHrs = 0;
    int totalMins = 0;
    int totalSecs = 0;

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

       int add = (totalHrs, totalMins, totalSecs);
       printf ("%d x %d = %d\n", totalHrs, totalMins, totalSecs, add);
       int normalize(int sec, int min, int hrs);
       int twoDigits(char sec, char min, char hrs);
       int printTime(int totalHrs, int totalMins, int totalSecs);
    }


      return 0;

}

// Normalize the time and print the song
int normalize (int sec, int min, int hrs)
    {
	sec = sec % 60;
    min = sec / 60;
    hrs = min / 60;
    }

// Add this time to the sum.
// Keep the sum normalized
//(useful for debugging purposes)

int add (int sec, int min, int hrs, int totalHrs, int totalMins, int totalSecs)
      {
      int result;
      result = totalSecs += sec;
      result = totalMins += min;
      result = totalHrs += hrs;
      result = totalMins = totalSecs % 60;
      result = sec = totalSecs %60;
      result = totalHrs = totalMins /60;
      return result;
      }

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

// Convert an integer in the range of 0..99 into a string containing exactly two characters
int twoDigits (char sec, char min, char hrs)
      {
          char buffer [99] = {0};
          sprintf (buffer, "%i", sec);
          sprintf (buffer, "%i", min);
          sprintf (buffer, "%i", hrs);
      }
closed account (zb0S216C)
R2B Boondocks wrote:
 
int add = (totalHrs, totalMins, totalSecs);

This is not a function invocation (call). This is in fact a declaration of a variable, called "add", which is initialised with the value of "totalSecs." Remove "int", and the equals symbol, and you'll be good to go. Do this for all function calls that appear in this way.

Wazzak
I changed them to:
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
int main (void)
{
    int totalHrs = 0;
    int totalMins = 0;
    int totalSecs = 0;

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

       add (totalHrs, totalMins, totalSecs);
       printf ("%d x %d = %d\n", totalHrs, totalMins, totalSecs, add);
       normalize(int sec, int min, int hrs);
       twoDigits(char sec, char min, char hrs);
       printTime(int totalHrs, int totalMins, int totalSecs);
    }


      return 0;

}


I now get these errors:

In function 'int main()':
error: 'add' was not declared in this scope
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: 'normalize' was not declared in this scope
error: expected primary-expression before 'char'
error: expected primary-expression before 'char'
error: expected primary-expression before 'char'
error: 'twoDigits' was not declared in this scope
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: 'printTime' was not declared in this scope

My proposed solution:
instead of
 
int main (void)

Do
 
int main (add, normalize, twoDigits, printTime)


So that they are "declared in this scope". Thoughts?


EDIT**
This did not work :/
Last edited on
I mean, the errors are self-explanatory, don't you think?

Did you go over a tutorial of C++, or you saw some code sitting there and went on to compile it?

I don't mean to sound rude, here are the error explanations:

error: 'add' was not declared in this scope

add is not declared in main() before it is used. Moreover, I honestly cannot tell what add is. At one point you're using it as a function, and at another point as a parameter!

error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: 'normalize' was not declared in this scope

You're writing a prototype for the function normalize() inside main()!

error: expected primary-expression before 'char'
error: expected primary-expression before 'char'
error: expected primary-expression before 'char'
error: 'twoDigits' was not declared in this scope

You're writing a prototype for the function twoDigits() inside main()!

error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
error: 'printTime' was not declared in this scope

You're writing a prototype for the function printTime() inside main()!


Kindly go over this small introduction to functions
http://www.cplusplus.com/doc/tutorial/functions/

Last edited on
Self explanatory yes to an individual who has a sense of what they're doing. Much like a wiring schematic for a vehicle remote start being easily read to me since that is what I do. I will be honest with you. I do not know what I am doing in C++ so I took no offense from your statements.

I will review your link and see if I can't figure this out. Thank you.
I believe I have conquered the function issue :). Thank you all for the help. I'd like to ask one last question though. The code runs fine and works perfectly up until I signify end of user input (ctrl-z). It then spits out the total song times. It works in the sense that it keeps the remainders for seconds, minutes, etc. However, it does not add those increments of 60 over to the next value. For example:

0 61 0 Time
// User sees this output// Time 1:01:00
0 59 Money
// User sees this output //Money 0 59:00
ctrl-z //signifying end of user input
Total: 1:00:00

The hours should read 2 here. I'm sure it is something simple. 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;


// Normalize the time and print the song
void normalize (int &sec, int &min, int &hrs)
{
      int m = sec / 60;
      sec = sec % 60;
      min += m;
      int h = min / 60;
      hrs += h;
      min = min % 60;
}

// Pad the title to 40 characters
void twoDigits (int sec, int min, int hrs, string title)
{
      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)
void add (int &sec, int &min, int &hrs,int &totalSecs,int &totalMins, int &totalHrs)
{
      totalSecs += sec;
      totalMins += min;
      totalHrs += hrs;
      min = totalSecs / 60;
      totalSecs = totalSecs % 60;
      hrs = totalMins / 60;
      totalMins = totalMins % 60;
}

  // Done with all the songs. Print the total time.
void printTime (int &totalSecs, int &totalMins, int &totalHrs)
{
  cout << "Total: ";

  cout << totalHrs << ':';
  if (totalMins < 10)
    cout << '0';
  cout << totalMins << ':';
  cout << setfill('0') << setw(2) << totalSecs;
  cout << endl;
}

int main ()
{
  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(sec, min, hrs);

twoDigits(sec, min, hrs, title);

add (sec, min, hrs, totalSecs, totalMins, totalHrs);
    }

printTime (totalSecs, totalMins, totalHrs);

  return 0;

}


Thank you
Topic archived. No new replies allowed.