I am writing a program for class where it converts from military time to standard. Everything else seems to be working fine until I type in 60 for the minutes. For example if I type 23:60 it gives me 11:60 PM which is incorrect. How do I fix this?
#include <iostream>
using namespace std;
void inputData(int&, int&, char&); // this function asks users to input information
void convertData(int&, int&, char&); // this converts the military time to standard time
int outputData(int&, int&, char&); // this function puts all the other information together to output certain data
int main ()
{
int hours, minutes;
char am_pm;
char trueValue;
do
{
inputData(hours, minutes, am_pm); // calls to input function
convertData(hours, minutes, am_pm); // calls to the conversion function
outputData(hours, minutes, am_pm); // calls to function that outputs all the data
cout << "Would you like another conversion? Type Y or y to repeat." << endl;
cin >> trueValue;
}
while (trueValue == 'y'|| trueValue == 'Y');
if (trueValue != 'y' || trueValue != 'Y')
cout << "Thanks for using this converter. Have a nice day." << endl;
return 0;
}
void inputData (int &hours, int &minutes, char &am_pm)
{
cout << "Please enter hours (less than or equal to 24): "; // ask user to input hours.
do
{
cin >> hours;
if (hours > 24)
cout << "ERROR! Must be less than 24" << endl;
}
while (hours > 24); // end of hours loop
cout << "Please enter minutes (less than or equal to 60): "; // ask user to input minutes.
do
{
cin >> minutes;
if (minutes > 60)
{
cout << "Must be less than 60. Try again!" << endl;
}
}
while (minutes > 60); //end of minutes loop
cout << "You have entered: " << hours << ":" << minutes; // display what user inputs together.
cout << endl;
}
void convertData(int &hours, int &minutes, char &am_pm)
{
if (hours < 12)
{
hours = 12-12+1;
}
if (hours > 12)
{
hours = hours - 12; // subtracts anything bigger than 12 to get standard time. Anything over 12 is PM according to project instruction
am_pm = 'P';
}
else
if (hours == 12)
{
am_pm = 'P';
}
else
am_pm = 'A';
}
int outputData(int &hours, int &minutes, char &am_pm)
{
if (am_pm == 'P')
cout <<"Your standard time is: " << hours << ":" << minutes << " P.M" << endl;
else
cout <<"Your standard time is: " << hours << ":" << minutes << " A.M" << endl;
}
For your conversion, check if minutes == 60, then add an additional hour and reset minutes to 0. You have to remember if you add an additional hour 23:60 will then turn into 12 A.M and not 12 P.M.
You might want to consider looking at it another way.
A standard timepiece is only interested in a day. After 24 hours it wraps back around to the first hour. In 24/military format, that’s:
0:00, 1:00, 2:00, ..., 22:00, 23:00 ...and wrap back to 0:00
We do this kind of thing normally with a 12-hour clock. If it is 11:30, and I want to know what time it is three hours from now, I can compute the answer by adding three and subtracting twelve:
11:30 + 3:00 = 14:30
14:30 - 12:00 = 2:30
This wrap-around behavior is called “modular arithmetic”. All that really means is that you are using the remainder of a division every time you do something. So, to figure out what time it is N hours from now, we can use the following equation:
result = (now + N) % 12
(That % is C++’s remainder operator. Mathematicians will write it as “result ≡ now + N (mod 12)”, just in case you see that somewhere.)
So I can add 37159 hours to 11:30, and I know it will be 6:00 (four and a half years from now).
Here is the cool part. One hour is 60 minutes each. So if I wanted to represent the time in minutes, I would have to multiply the hour by 60 and simply add the remaining minutes.
11:27 → 11 * 60 + 27 → 687 minutes into the day.
To get it back, I only need to divide for the hours, and use the remainder for the minutes:
687 / 60 → 11 (remember, integer arithmetic throws out that 0.45) 687 % 60 → 27
At this point, I can represent a day’s time very easily as the number of minutes in the day (remainder (24*60), because there are 24*60 minutes in a day).
To convert that to a 24-hour time, I only need divide by 60 and get the quotient and remainder:
1057 / 60 → 17
1057 % 60 → 37
time is 17:37
To convert to a 12-hour time, I only need to add an additional division in there:
17 / 12 → 1 (it is PM) 17 % 12 → 5
time is 5:37 PM
You could also throw that remainder in before you start. Just remember that if you multiply one side of an equation you have to multiply the other side by the same value.
1057 / (60*12) → 1 (it is PM) 1057 % (60*12) → 337 (337 minutes into the PM) 337 / 60 → 5
337 % 60 → 37
time is 5:37 PM
There is one caveat remaining. 12 % 12 is zero. We don’t like zeros on our 12-hour clocks. Instead of zero, we use... 12! That’s right, replace a 12 with a zero and you have a mathematical clock. So whenever you get zero, you just need to substitute 12. And you only have to worry about that conversion when going from minutes to hours.
if (hour == 0) hour = 12;
However you do it, just use the mathematical properties of time and apply your equations.
Hope this helps.
#include <iostream>
usingnamespace std;
void inputData(int &, int &, char &); // this function asks users to input information
void convertData(int &, int &, char &); // this converts the military time to standard time
int outputData(int &, int &, char &); // this function puts all the other information together to output certain data
int
main()
{
int hours, minutes;
char am_pm;
char trueValue;
do {
inputData(hours, minutes, am_pm); // calls to input function
convertData(hours, minutes, am_pm); // calls to the conversion function
outputData(hours, minutes, am_pm); // calls to function that outputs all the data
cout << "Would you like another conversion? Type Y or y to repeat." << endl;
cin >> trueValue;
}
while (trueValue == 'y' || trueValue == 'Y');
if (trueValue != 'y' || trueValue != 'Y')
cout << "Thanks for using this converter. Have a nice day." << endl;
return 0;
}
void
inputData(int &hours, int &minutes, char &am_pm)
{
cout << "Please enter hours (less than or equal to 24): "; // ask user to input hours.
do {
cin >> hours;
if (hours > 24)
cout << "ERROR! Must be less than 24" << endl;
}
while (hours > 24); // end of hours loop
cout << "Please enter minutes (less than or equal to 60): "; // ask user to input minutes.
do {
cin >> minutes;
if (minutes > 60) {
cout << "Must be less than 60. Try again!" << endl;
}
}
while (minutes > 60); //end of minutes loop
cout << "You have entered: " << hours << ":" << minutes; // display what user inputs together.
cout << endl;
}
void
convertData(int &hours, int &minutes, char &am_pm)
{
if (hours < 12) {
hours = 12 - 12 + 1;
}
if (hours > 12) {
hours = hours - 12; // subtracts anything bigger than 12 to get standard time. Anything over 12 is PM according to project instruction
am_pm = 'P';
} elseif (hours == 12) {
am_pm = 'P';
} else
am_pm = 'A';
}
int
outputData(int &hours, int &minutes, char &am_pm)
{
if (am_pm == 'P')
cout << "Your standard time is: " << hours << ":" << minutes << " P.M" << endl;
else
cout << "Your standard time is: " << hours << ":" << minutes << " A.M" << endl;
}
Line 25 can be removed. If you exit the while loop above it, then the condition must be true.
Line 34 says that the value for hours can be equal to 24. Except for line 38, the code matches this comment. In other words, you're code says that 24:55 is a valid time. Is it?
You have the same problem with line 42 and the code that follows. It allows a minute value of 60.
Taken together, these two lines mean that times like 8:60, 24:18 and 24:60 are valid.
You might also check for negative numbers. Right now your code will accept 8:-3, or -8:-3, or even -43854322:-58578344 as valid times.
Your loops are checking the condition twice. You can simplify it a little like this:
1 2 3 4 5
while (true) {
cin >> hours;
if (hours <= 24) break;
cout << "ERROR! Must be less than 24" << endl;
}
Line 58 always sets hours to 1.
I suggest that you get lines 34-40 working correctly. Once that code is right, make the corresponding changes to lines 42-49.