Help with functions to convert 24-hour notation into 12-hour notation

Hi, would someone mind taking a look at how this could have been done better? Someone mentioned to me " You should be careful about passing hours and minutes by reference into your functions unless you actually mean to change the values inside the functions."

However, the program won't calculate the time changes unless I use the call by references for these functions:

void validate(int& hours, int& mins)
void swap_values(int& hours, int& new_hours)

Also, if the entered "hour" is less than 12( for instance, 11:44) then new_hour will be a negative number. I'm not sure how to fix that.

I tried to tailor all the functions in the program so they can be used interchangeably in different programs that could calculate different times.

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
93
94
95
96
97
98
99
100
101
102
103
104
105

// Write a program that converts the 24-hour notation into 12-hour notation.
//14:25 shall be read as 14  25 and be converted to 2:25 PM. 
//Validate the input first. Hour(s) should not be less than zero or greater than 23. 
//Minutes should be between zero and 59.


#include <cstdlib>
#include <iostream>
using namespace std;

void validate(int& hours, int& mins);
//Function will display to user their 24-hours time if the time they enter doesn’t have hours 
//less than zero or greater than 23 AND the minutes they enter is between zero and 59.
//Otherwise, function will prompt reentry until both valid hours and minutes are entered.
//If user enters zeros for hours and minutes the program ends.

int subtraction(int hours, int hourchange);
//Function will take the user entered 24-hours time hours and subtract 12 to get 
//the 12-hours notation hours, thus new variable new_hours.

void swap_values(int& hours, int& new_hours);
//Function will swap out the 12-hours notation hours for the 24-hours time hours time.
//Swaps values stored in new_hours and hours.

void show_twelvehours(int hours, int mins);
//Function will display the 12-hours notation.

void show_twentyfourhours(int new_hours, int mins);
//Function will display the 24-hours notation.

int hours = 0;
int mins = 0;
int new_hours;
int hourchange = 12;

int main()

{
	do
	{	//function calls
		validate(hours, mins);
		new_hours = subtraction(hours, hourchange);
		swap_values(hours, new_hours);
		show_twelvehours(hours, mins);
		show_twentyfourhours(new_hours, mins);
	} while (hours != 0);
	return 0;
}

//Function Definitions
void validate(int& hours, int& mins)
{
	cout << "Enter a number for hours and another number for minutes." << endl;
	cin >> hours;
	cin >> mins;

	if (hours == 0)
	{
		cout << "Zeros end the program." << endl;
		exit(0);
	}

	while (hours < 0 || hours > 23)
	{
		cout << "Reenter hours." << endl;
		cin >> hours;
	}

	while (mins < 0 || mins > 59)
	{
		cout << "Reenter minutes." << endl;
		cin >> mins;
	}
}

int subtraction(int hours, int hourchange)
{	
	new_hours = hours - hourchange;
	return new_hours;
}

void swap_values(int& hours, int& new_hours)
{
	int temp;
	temp = hours;
	hours = new_hours;
	new_hours = temp;
}

void show_twelvehours(int hours, int mins)
{	
	if (mins < 10)
		cout << "Your 12-hour time is " << hours << ":0" << mins << endl;
	else
		cout << "Your 12-hour time is " << hours << ":" << mins << endl;
}

void show_twentyfourhours(int new_hours, int mins)
{	
	if (mins < 10)
		cout << "Converted back to 24-hour time is " << new_hours << ":0" << mins << endl;
	else
		cout << "Converted back to 24-hour time is " << new_hours << ":" << mins << endl;
}
Last edited on
Your negative hours problem arises from the fact that there is overlap between 12-hour and 24-hour time. 01:00 in 24-hour time is the same as 1:00 (am) in 12-hour time, so that's a condition that you must check for (from 01:00 to 12:00 in 24-hour time).
Remember, when you're converting 24 hour notation to the 12 hour notation you would only use your subtraction function if the time is greater than 1200 or 12:00. For example 1800 would 6:00 pm. 0500 would be 5:00 am. No need to use your subtraction function in the 0500 case. I'm pretty sure that's why you're getting a negative number.
Topic archived. No new replies allowed.