Unsure how to pass a value to a function with two reference paramaters!

EDIT: How would I pass timeTotal hours_minutes(int timeInMinutes, int &h, int &m)?

Hello,

I have an assignment with the following instructions:

"Write a main program that gets from the user two times as hours/minutes, adds them, and prints the resulting time as hours and minutes. If the user enters 1 hour 50 min and 2 hours 30 min the result will be 4 hours 20 minutes.

Write two functions:

----

[1] The first, total_minutes(), takes two integer parameters, a time in hours and minutes, and returns the total number of minutes

For example, total_minutes(2, 15) returns 135.

----

[2] The second function hours_minutes() takes three parameters. The first is a value parameter that is a time in minutes, the second two are reference parameters that will be set to the number of whole hours and remaining minutes in the first parameter.

For example, hours_minutes(135, h, m) will set h to 2 and m to 15.

----

How would I use the pointers correctly with each time? Something in my mind is tangled up, so I left the main function incomplete so I don't dig a deeper hole. Also as a reminder, my attempt below, including the comments, are my interpretation and may be incorrect so please remember that so you don't confuse yourself with the instructions and what I think it means! Thank you in advance!

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
#include <iostream>
using namespace std;

int total_minutes(int hours, int minutes);
int hours_minutes(int timeInMinutes, int* h, int* m);

int main()
{
    int time1, hours1, minutes1;
    int time2, hours2, minutes2;
    int timeTotal;

    cout << "I will ask for two times' respective hours and minutes. \n\nEnter the HOURS of the FIRST time:" << endl;
    cin >> hours1;
    cout << "Now enter the MINUTES of the FIRST time:" << endl;
    cin >> minutes1;

    time1 = total_minutes(hours1, minutes1); //Finding total minutes of time1

    cout << "\nEnter the HOURS of the SECOND time:" << endl;
    cin >> hours2;
    cout << "Now enter the MINUTES of the SECOND time:" << endl;
    cin >> minutes2;

    time2 = total_minutes(hours2, minutes2); //Finding total minutes of time2

    timeTotal = time1 + time2; //Times added in minutes !! HOW TO PASS TO hours_minutes? !!

    return 0;
}

int total_minutes(int hours, int minutes) //Converts hours and minutes to only minutes
{
    int minutesFromHours;

    minutesFromHours = (hours * 60) + minutes;

    return minutesFromHours;
}

void hours_minutes(int timeInMinutes, int &h, int &m) //Adds the two inputted times in minutes and converts them to hours
{
    h = (timeInMinutes / 60);  //Gives how many whole hours
    m = (timeInMinutes % 60);  //Gives how many minutes were remaining

    cout << "Your added time is " << h << "h and " << m << "m.";
}
Last edited on
Get two times from user. For example, 1h 50min and 2h 30min.
Convert the two times into minutes using total_minutes().
Add them together.
Pass that to hours_minutes().
Print out resulting time.

1
2
3
4
5
void hours_minutes(int timeInMinutes, int* h, int* m) //Adds the two inputted times in minutes and converts them to hours
{
    h = (timeInMinutes / 60);  //Gives how many whole hours
    m = (timeInMinutes % 60);  //Gives how many minutes were remaining
}

Also, as h and m are pointers, you need to dereference them before assigning their values.
Alternatively, you can use C++ references, so that you don't have to deal with pointers.
void hours_minutes(int timeInMinutes, int& h, int& m)
Thanks for your response!

I actually just updated my post with my progress. How would you explain the declaration of int&h? Does it just mean "the declaration of the memory address of h?"

EDIT: Also, when you say "pass that to hours_minutes()," how would I pass timeTotal hours_minutes(int timeInMinutes, int &h, int &m);? I'm not exactly sure how to do this.
Last edited on
How would you explain the declaration of int&h?

A reference to an int. Basically, it allows you to use h as if it were the original variable passed in.
1
2
3
4
5
6
7
8
9
10
11
void foo( int& n ) 
{
    n = 10;
}

int main( )
{
    int a = 1;
    foo( n );
    // a is now 10
}



EDIT: Also, when you say "pass that to hours_minutes()," how would I pass timeTotal hours_minutes(int timeInMinutes, int &h, int &m);? I'm not exactly sure how to do this.
1
2
3
4
timeTotal = time1 + time2; //Times added in minutes !! HOW TO PASS TO hours_minutes? !!
// like this
int total_hrs = 0, total_mins = 0;
hours_minutes( timeTotal, total_hrs, total_mins );


Also you don't need to declare time1 and time2. You can just do this:
 
timeTotal = total_minutes(hours1, minutes1) + total_minutes(hours2, minutes2);


You could also use a for loop and arrays to prevent copy pasting the same code.
1
2
3
4
5
6
7
8
const int NUM_TIMES = 2;
int times[NUM_TIMES] = { 0 }; // sets all the elements to 0
for( int i = 0; i < NUM_TIMES; i++ ) {
    cout << "Time " << i << " hours: ";
    cin >> times[i];

    // do the same for minutes
}
Great explanation! I finally understand!

Thank you so much for all your help! :)
Topic archived. No new replies allowed.