Standard To Military Time

I'm pretty sure something is wrong either in my timeOfDay() or convertTime(), because what the heck.



#include "stdafx.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void convertTime(int h, int m, char ampm);
int getMinute();
int getHour();
char getTimeOfDay();

int _tmain(int argc, _TCHAR* argv[])
{

cout <<"\t\t\t"<< "Military Time Converter:" << "\t\t\t" << endl;
int m = getMinute();
int h = getHour();
char ampm = getTimeOfDay();
cout << "Standard time: ";
cout << h << ":" << m <<" " << ampm<<"M" <<endl;
cout << endl;
cout << endl;
cout << "Military time: ";
convertTime(h, m, ampm);

}

int getMinute()
{
int minute;
cout << "Please enter minute: ";
cin >> minute;
if (minute < 0 || minute > 60)
{
cout << "Please enter a valid minute.";
getMinute();
}
return minute;
}

int getHour()
{
int hour;
cout << "Please enter hour: ";
cin >> hour;
if (hour < 1 || hour > 12)
{
cout << "Please enter a valid hour.";
}
return hour;
}

char getTimeOfDay()
{
char time;
cout << "AM or PM (A or P): ";
cin >> time;
char aOrP = toupper(time);
if (aOrP != 'A' || aOrP != 'P')
{
cout << "Pleas enter a valid time of day. ";
}
return aOrP;
}


void convertTime(int h, int m, char ampm)
{
int displayH = h;
if (h == 12 && ampm == 'A')
{
cout << "00:" << m << ampm << endl;
}
else if (ampm == 'P')
{
for (int i = 13; i < h; i++)
{
displayH = i;
}
}
else
{
displayH = h;

if (displayH > 12)
{
cout << "0:" << displayH << ":" << m << " " << ampm;
}
else
{
cout << displayH << ":" << m << " " << ampm;
}
}
}
Last edited on
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; 
}





Last edited on
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?

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
#include "stdafx.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void convertTime(int h, int m, char ampm);
int getMinute();
int getHour();
char getTimeOfDay();

int _tmain(int argc, _TCHAR* argv[])
{

	cout <<"\t\t\t"<< "Military Time Converter:" << "\t\t\t" << endl;
	int m = getMinute();
	int h = getHour();
	char ampm = getTimeOfDay();
	cout << "Standard time: ";
	cout << h << ":" << m <<" " << ampm<<"M" <<endl;
	cout << endl;
	cout << endl;
	cout << "Military time: ";
	convertTime(h, m, ampm);

}

int getMinute()
{
	int minute;
	cout << "Please enter minute: ";
	cin >> minute;
	if (minute < 0 || minute > 60)
	{
		cout << "Please enter a valid minute.";
		getMinute();
	}
	return minute;
}

int getHour()
{
	int hour;
	cout << "Please enter hour: ";
	cin >> hour;
	if (hour < 1 || hour > 12)
	{
		cout << "Please enter a valid hour.";
		getHour();
	}
	return hour;
}

char getTimeOfDay()
{
	char time;
	cout << "AM or PM (A or P): ";
	cin >> time;
	char aOrP = toupper(time);
	if (aOrP != 'A' && aOrP != 'P')
	{
		cout << "Please enter a valid time of day. ";
	}
	return aOrP;
}


void convertTime(int h, int m, char ampm)
{
	int displayH = h;
	if (h == 12 && ampm == 'A')
	{
		cout << "00:" << m << ampm << endl;
	}
	else if (ampm == 'P')
	{
		displayH = +13; //Fixed the loop so the military hour would be correct in the event of a PM hour
	}
	else
	{
		displayH = h;

		if (displayH < 10) //Found a dumb error here
		{
			cout << "0:" << displayH << ":" << m << " " << ampm;
		}
	}

		
}
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.

try this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void convertTime(int h, int m, char ampm)
{
	int displayH = h;
	if (h == 12 && ampm == 'A')
	{
		displayH = 0;
	}
	else if (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;
}
Last edited on
@STK - Line 10 is not correct. if 12pm is entered, your result is 25:00. 1pm becomes 14:00, etc. See the code I posted above.
@Niscuit

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.

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
#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;
}
Last edited on
closed account (48T7M4Gy)
HINT 1: Write some pseudocode before the C++ coding

0 <= hours < 12
0 <= minutes < 60
toupper(ampm) == 'A' or ... 'P'

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
}





Last edited on
@kemort

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'.
closed account (48T7M4Gy)
Oops - all fixed, thanks w1
Topic archived. No new replies allowed.