First, let me get this out of the way. I know my program is a little rough right now, but I can't even get to fixing it if lines 42-52 (future time input) are being skipped. So, for context, I need to make a time machine that can go forward 24 hours, and I need to make sure that the input for the start and future time is in the HH:MM xm format. Then I need to have the total minute difference, which can convert to the hours and minutes until the future time. Sorry if what I've done gives you a headache still learning.
Here is my code:
#include <iostream>
// Total number of minutes in an hour and day
const int MINUTES_IN_HOUR = 60;
const int MINUTES_IN_DAY = 24 * MINUTES_IN_HOUR;
// Function that computes the difference between two times
int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar);
int main()
{
using namespace std;
int startHours;
int startMinutes;
bool startIsAM;
int endHours;
int endMinutes;
bool endIsAM;
char amPmChar;
char extra; //IMPORTANT!!
char answer;
int diff;
do
{
cout << "Enter start time, in the format 'HH:MM xm', where xm is \n" << "either 'am' or 'pm' for AM or PM: ";
cin >> startHours >> extra >> startMinutes >> startIsAM;
if (startIsAM == 'a' || startIsAM == 'A')
{
startIsAM = true;
}
else
{
startIsAM = false;
}
cout << "\nEnter future time, in the format 'HH:MM xm', where xm is\n" << "either 'am' or 'pm' for AM or PM: ";
cin >> endHours >> extra >> endMinutes >> endIsAM;
if (endIsAM == 'a' || endIsAM == 'A')
{
endIsAM = true;
}
else
{
endIsAM = false;
}
diff = convertDifference(startHours, startMinutes, startIsAM, endHours, endMinutes, endIsAM);
/*if ((hours > 1) && (minutes > 1))
{
cout << "There are " << diff << " minuets " << "(" << hours << " hours and " << minutes << " minutes) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
}
else
{
cout << "There are " << diff << " minuets " << "(" << hours << " hour and " << minutes << " minute) between " << startHours << ":" << startMinutes << " and " << endHours << ":" << endMinutes << ".";
}
*/
cout << endl << "\nRun again? (y / n): ";
cin >> answer;
} while (answer == 'y');
return 0;
}
int convertDifference(int startHoursPar, int startMinutsPar, bool startIsAMPar, int endHoursPar, int endMinutesPar, bool endIsAMPar)
{
int diff, hours, minutes, minutes2;
if (startIsAMPar == 'p')
{
if (startHoursPar >= 1 && startHoursPar < 12)
{
startHoursPar += 12;
}
}
if (endIsAMPar == 'p')
{
if ((startHoursPar >= 1) && (endHoursPar < 12))
{
endHoursPar += 12;
}
}
minutes = (startHoursPar * 60) + startMinutsPar;
minutes2 = (endHoursPar * 60) + endMinutesPar;
if ((startHoursPar >= endHoursPar) || ((startHoursPar == endHoursPar) && (startMinutsPar > endMinutesPar)))
{
endMinutesPar += MINUTES_IN_DAY;
}
diff = endMinutesPar - startMinutsPar;
if (diff > MINUTES_IN_DAY)
{
diff -= MINUTES_IN_DAY;
}
return diff;
}