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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <string>
using namespace std;
int computeDifference(int hour1, int hour2, int min1, int min2,
bool isAm1, bool isAm2);
int main ()
{ int hour1, hour2, min1, min2;
bool isAm1 = 0, isAm2 = 0;
char time1, time2, answer, ch;
do
{
cout << "Enter start time in 'HH:MM xm' format, where 'xm' is 'am' or 'pm' for AM or PM =>";
//Store : and m as character values
cin >> hour1 >> ch >> min1 >> time1 >> ch;
if(hour1 > 12)
{
cout << "Invalid hour number. Please enter an hour between 1 and 12 =>";
cin >> hour1;
}
if(min1 > 59)
{
cout << "Invalid minute number. Please enter a minute between 0 and 59 =>";
cin >> min1;
}
if ((time1 == 'a')|| (time1 == 'A'))
{
isAm1 = 1;
}
cout << "Enter start time in 'HH:MM xm' format, where 'xm' is 'am' or 'pm' for AM or PM =>";
cin >> hour2 >> ch >> min2 >> time2 >> ch;
if (hour2 > 12)
{
cout << "Invalid hour number. Please enter an hour between 1 and 12 => ";
cin >> hour2;
}
if (min2 > 59)
{
cout << "Invalid minute number. Please enter a minute between 0 and 59 =>";
cin >> min2;
}
if ((time2 == 'a')||(time2 == 'A'))
{
isAm2 = 1;
}
cout << "There are " << computeDifference(hour1, hour2, min1, min2, isAm1, isAm2) << " minutes (" << (computeDifference(hour1, hour2, min1, min2, isAm1, isAm2))/60 << " hours and " << (computeDifference(hour1, hour2, min1, min2, isAm1, isAm2))%60 << " minutes) between " << hour1 << ":" << min1 <<" " << time1 << "m and " << hour2 << ":" << min2 <<" " << time2 <<"m" << endl;
cout << "Enjoy the future!" << endl;
cout << "Would you like to restart the operation? Y/N ";
cin >> answer;
}
while ((answer == 'y')|| (answer == 'Y'));
return 0;
}
int computeDifference(int hour1, int hour2, int min1, int min2, bool isAm1, bool isAm2)
{
int Difference, minutes1, minutes2;
if(isAm1)
{
if ((hour1 >= 1) && (hour1 < 12))
{hour1 += 12;}
}
if(isAm2)
{
if ((hour2 >= 1) && (hour2 < 12))
{hour2 += 12;}
}
minutes1 = (hour1 * 60) + min1;
minutes2 = (hour2 * 60) + min2;
if ((hour1 >= hour2) || ((hour1 == hour2) && (min1 > min2)))
{
minutes2 += 1440;
}
Difference = minutes2 - minutes1;
if (Difference > 1440)
{
Difference -= 1440;
}
return Difference;
}
|