Hi Everyone,
I'm new and I'm stuck. I will try to make sure that I post this correctly. The assignment is:
Create at least two function beside the main function. One to take user input and one to display the output. It is your option to create a 3rd
function to do the computation or do the computation in the main(). The
program must also handle up to 24 hours of waiting time,ie (the next day).
For example if the start time is 22:00 and the wait time is 4:10. then
the correct result is 2:10, not 26:10. (Modulus operator helps here)
You will need to use reference parameters to do handle user input.
I think that I have everything I need. When I do my calculation I can get the correct result for the above example, but with any other input I do not get the correct results i.e. 13:25 current time and 2:20 added time = 15:45 new time. Two questions first am I going in the right direction for my calculation? Secondly, I can not get a colon to work with my input of hours and minutes, I have to use a space in between the hours and minutes i.e. 22 00 and 4 10 and suggestions on how to get this to work properly without having the hours and minutes appear on seperate lines? Thanks for any suggestions. Here is the code I have so far:
Code:
#include <iostream>
using namespace std;
// function declarations
void get_input(int& hours, int& minutes);
int wait_time(int hours, int minutes);
void give_output(int hours, int minutes, int wait_time);
int main()
{
int current_time, added_time, new_time;
char answer;
do
{
get_input(current_time, added_time);
new_time = wait_time(current_time, added_time);
give_output(current_time, added_time, new_time);
cout << endl << "Enter Y or y to continue, any other halts program: ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
}
void get_input(int& hours, int& minutes)
{
cout << "Compute completion time from current time and waiting period.\n"
<< "Current time:\n"
<< "Enter 24 hour time in the format HH:MM\n";
cin >> hours >> minutes;
cout << endl << endl << "Waiting time:\n"
<< "Enter 24 hour time in the format HH:MM\n";
cin >> hours >> minutes;
}
void give_output(int hours, int minutes, int new_time)
{
cout << endl << "Completion Time in 24 hour format: " << new_time << ":";
// special handling for leading 0s on the minutes
cout.width(2);
cout.fill('0');
cout << minutes;
}
int wait_time(int hours, int minutes)
{
int new_time, current_time, added_time;
if (new_time > 24)
{
hours = current_time + added_time;
new_time = hours % 24;
}
else
{
new_time = current_time + added_time;
}
}