Your time machine is capable of going forward in time up to
Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time (in hours, minutes and a Boolean indicating AM or PM) and a future time (in hours, minutes and a Boolean indicating AM or PM), and calculate the difference in minutes between the start and future time.
A time is specified in your program with three variables.
For example, to represent 11:50 PM you would store:
This means that you need six variables to store a start and future time.
Write a program that allows the user to enter a start time and a future time. Include a function named computeDifference() that takes the six variables as parameters that represent the start time and future time. Your function should return an int, the time difference in minutes. For example, given a start time of 11:59 AM and a future time of 12:01 PM, your program should compute 2 minutes as the time difference. Given a start time of 11:59 AM and a future time of 11:58 AM, your program should compute 1439 minutes as the time-difference (23 hours and 59 minutes). This program should be done using functions.
**As an addition, read in two characters (such as \'A\' and \'P\') that represent AM. And allow user to run program as often as they like or exit when they like.
Solution
#include <iostream>
using namespace std;
int computeDifference(int hours1,int hours2,int mins1,int mins2,bool isPM1,bool isPM2);
int main()
{
int hours1,hours2,mins1,mins2;
bool isPM1 = 0, isPM2 = 0;
char time1, time2;
cout << \"Please enter your time in hour: \ \";
cin >> hours1;
cout << \"Please enter your time in minute: \ \";
cin >> mins1;
cout << \"Please enter for AM or PM (enter A for AM; enter P for PM) \ \";
cin >> time1;
if ((time1 == \'p\') || (time1 == \'P\'))
{
isPM1 = 1;
}
cout << \"Please enter your future time in Hours: \ \";
cin >> hours2;
cout << \"Please enter your future time in Minutes: \ \";
cin >> mins2;
cout << \"Please enter for AM or PM (enter A for AM; enter P for PM) \ \";
cin >> time2;
if ((time2 == \'p\') || (time2 == \'P\'))
{
isPM2 = 1;
}
cout << \"Time difference is: \";
cout << computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2);
if (computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2) == 1)
cout << \" minute.\ \";
else
cout << \" minutes.\ \";
system(\"PAUSE\");
return 0;
}
int computeDifference(int hours1, int hours2, int mins1, int mins2, bool isPM1, bool isPM2)
{
int total_difference, minute1_total, minute2_total;
if (isPM1)
{
if ((hours1 >= 1) && (hours1 < 12))
{
hours1 += 12;
}
}
if (isPM2)
{
if (hours2 >= 1 && hours2 < 12)
{
hours2 += 12;
}
}
minute1_total = (hours1 * 60) + mins1;
minute2_total = (hours2 * 60) + mins2;
if ((hours1 >= hours2) || ((hours1 == hours2) && (mins1 > mins2)))
{
total_mins2 += 1440;
}
total_difference = minute2_total - minute1_total;
if (total_difference > 1440)
{
total_difference -= 1440;
}
return total_difference;
}

