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.";
}
|