181k views
4 votes
Can someone help me code this with this output?

Write a program to convert minutes to time (separate hours/minutes). Include a user-defined void function named minutesToTime that takes an integer number of minutes and converts it to two separate integer values that represent the equivalent number of hours and minutes. You must use reference parameters (pass by reference) for the hours and minutes. Before proceeding further, discuss why this function uses reference parameters.
void minutesToTime(int minute_value, int& hours, int& mins)
Your main function needs to do the following:
- Prompt the user to input an integer number of minutes from the console.
- Call minutesToTime to compute the equivalent number of hours and minutes.
- Display the result on the terminal display using a 'colon' character to separate hours and minutes. Moreover, if the number of minutes is less than 10, print it out with a leading zero (e.g., 8 minutes would be printed as 08 rather than 8).
- Include a loop that will continue this process as long as the user wishes.
Example Output
Enter a number of minutes: 60
Hours:minutes is 1:00
Continue? (y/n): y
Enter a number of minutes: 8
Hours:minutes is 0:08
Continue? (y/n): y
Enter a number of minutes: 337
Hours:minutes is 5:37
Continue? (y/n): n

User AceMark
by
7.9k points

1 Answer

6 votes

Final answer:

The 'minutesToTime' function in C++ converts an integer number of minutes to hours and minutes using reference parameters. The provided code snippet includes user interaction, input of minute values, and displays the converted time in a user-friendly format.

Step-by-step explanation:

The function minutesToTime uses reference parameters to allow the function to modify the values of the variables passed to it, and ensure that the changes are reflected outside of the function's scope. To convert minutes to time, you need to divide the number of minutes by 60 to find the hours and then take the remainder as the minutes left over. Below is the code snippet using C++ that achieves the desired functionality, including the user input and a loop to continue the process based on user desire:


#include <iostream>

void minutesToTime(int minute_value, int& hours, int& mins) {
hours = minute_value / 60;
mins = minute_value % 60;
}

int main() {
char continueFlag = 'y';
int minutes, hours, mins;
while(continueFlag == 'y') {
std::cout << "Enter a number of minutes: ";
std::cin >> minutes;
minutesToTime(minutes, hours, mins);
std::cout << "Hours:minutes is " << hours << ":" << (mins < 10 ? "0" : "") << mins << std::endl;
std::cout << "Continue? (y/n): ";
std::cin >> continueFlag;
}
return 0;
}
User FredRoger
by
8.5k points