PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Following line numbers are approximate due to lack of code tags.
Line 32: You call get_minute recursively, but you ignore the result. If the input was invalid, you return the invalid value.
Line 45: If the input to get_hour is invalid, you continue as if the input was valid.
Line 52: Don't use || here. You want &&. If aOrP is "P', then aOrP != 'A' is true.
If the input is not valid, you continue as if it were.
Line 65: Why are you looping here?
convert_time can be a lot simpler:
61 62 63 64 65 66 67
void convert_time (int h, int m, char ampm)
{ if (h == 12 && ampm == 'A')
h = 0;
if (ampm == 'P' && h != 12)
h += 12;
cout << setw(2) << h << ":" << m << endl;
}
Thank you. I fixed what you pointed out, and for some reason it still isn't returning the military time. (just outputting Military Time: Press any key to continue) Also, could you clarify what was wrong with the minute/hour recursions are?
Line 18-19: It makes sense to reverse these two lines.
Line 35: You detect a minute value is not valid. You display a message and make a recursive call to getMinute(). getMinute should return a valid value, but you ignore the return value and fall through to line 40 which returns the initial invalid value.
Line 51: ditto
Line 62: If the value is not "A" or 'P', you fall through to line 66 which returns the invalid value. Add after line 64:
return getTimeOfDay();
Line 77-79: These lines are not correct. i.e. if 12pm is entered, your result is 25:00. 1pm becomes 14:00, etc. See the code I posted above.
Line 85: You don't display anything if displayH is >= 10.
void convertTime(int h, int m, char ampm)
{
int displayH = h;
if (h == 12 && ampm == 'A')
{
displayH = 0;
}
elseif (ampm == 'P')
{
displayH = +13; //Fixed the loop so the military hour would be correct in the event of a PM hour
}
if (displayH < 10) cout << "0";
cout << displayH;
if (m < 10) cout << "0";
cout << m << ampm;
}
Here's my version of displaying the military time. Of course, I'm not checking if the hours and minutes are legit or not. I just checked for 'A' and 'P' entry.
edit:
Corrected..
Made changes to if hours entered was 12 for am or pm. If 12 was entered for am, the military time should show 00, and a 12 if pm.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void convertTime(int h, int m, char ampm);
int main()
{
int hour, minute;
char AM_PM;
cout << "Please enter the hour.." << endl;
cin >> hour;
cout << "Now the minutes.." << endl;
cin >> minute;
cout << "Is the time 'A'm or 'P'm - (Enter either 'A' or 'P') ?" << endl;
do
{
cin >> AM_PM;
AM_PM = toupper(AM_PM);
} while (AM_PM != 'A' && AM_PM != 'P');
convertTime(hour, minute, AM_PM);
}
void convertTime(int h, int m, char ampm)
{
cout << "The time entered was " << h << ":" << m << " in the ";
if (ampm == 'A')
cout << "morning." << endl;
else
cout << "afternoon." << endl;
cout << endl << endl;
cout << "The military time, would be ";
if (ampm == 'A')
{
if (h == 12)
{
h = 0;
cout << "0";
}
if (h > 0 && h < 10)
cout << "0";
cout << h;
}
if (ampm == 'P')
{
if (h == 12)
h = 0; // Only if h is 12
cout << h + 12; // So that we don't get 24:00 for 12:30 in the afternoon
}
if (m < 10)
cout << "0";
cout << m << endl << endl;
}
Once the input has satisfied those conditions, to change to military time all that needs to be done is add 12 to hours if ampm is 'P'. minutes aren't affected.
HINT 2: Check out the while loop instead of if statements. I.e. keep asking for the particular hour, min etc until it meets the above condition.
while( condition not met )
{
prompt for value
input value
}
Once the input has satisfied those conditions, to change to military time all that needs to be done is add 12 to hours if ampm is 'A'. minutes aren't affected.
I believe you would add 12 to hours if the ampm is 'P', not 'A'.