This program is suppose to be a very easy code of a simple time machine. I have it all layed out and working, but the out put it not computing correctly.. I know it is something easy, but i have been staring at this code for a bit now, so it could easly be missed. what is wrong, or what am i missing?
here is my code:
#include <iostream>
using namespace std;
int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2); //calculates the time difference between the starting and ending time in minutes
int main()
{
int hours1, mins1, hours2, mins2;
bool isPM1 = 0, isPM2 = 0;
char time1, time2;
cout << "Please enter the starting hour: \n";
cin >> hours1;
cout << "Please enter the starting minute: \n";
cin >> mins1;
cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
cin >> time1;
if ((time1 == 'p') || (time1 == 'P'))
{
isPM1 = 1;
}
cout << "Please enter the hour you would like to travel to: \n";
cin >> hours2;
cout << "Please enter the minute of the hour you would like to travel to: \n";
cin >> mins2;
cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
cin >> time2;
if ((time2 == 'p') || (time2 == 'P'))
{
isPM2 = 1;
}
cout << "The time difference is: ";
cout << computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2);
if (computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2) == 1)
cout << " minute.\n";
else
cout << " minutes.\n";
system("PAUSE");
return 0;
}
int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2)
{
int difference, total_mins1, total_mins2;
if (isPM1)
{
if ((hours1 >= 1) && (hours1 < 12))
{
hours1 += 12;
}
}
if (isPM2)
{
if (hours2 >= 1 && hours2 < 12)
{
hours2 += 12;
}
}
total_mins1 = (hours1 * 60) + mins1;
total_mins2 = (hours2 * 60) + mins2;
if ((hours1 >= hours2) || ((hours1 == hours2) && (mins1 > mins2)))
{
total_mins2 += 1440;
}
difference = total_mins2 - total_mins1;
if (difference > 1440)
{
difference -= 1440;
}
return difference;
}