Time difference computation not functioning properly

Hello, I am writing a program which relies on the calculation of two different times difference, converted to minutes, input by the user. Currently, going from say 11:59am to 12:01pm calculates as 722 minutes instead of 2 for some reason.
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
  int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am) //Computes time differences.
{
	int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime;
	int time_difference;
	if (c_am == 1)
	{
		currentTime_hours = (c_hrs*60);
		currentTime_minutes = c_mins;
		currentTime = currentTime_hours + currentTime_minutes;
	}
	else if (c_am == 0)
	{
		currentTime_hours = ((c_hrs + 12) * 60);
		currentTime_minutes = c_mins;
		currentTime = currentTime_hours + currentTime_minutes;
	}
	if (t_am == 1)
	{
		targetTime_hours = t_hrs * 60;
		targetTime_minutes = t_mins;
		targetTime = targetTime_hours + targetTime_minutes;
	}
	else if (t_am == 0)
	{
		targetTime_hours = ((t_hrs + 12) * 60);
		targetTime_minutes = t_mins;
		targetTime = targetTime_hours + targetTime_minutes;
	}
	time_difference = targetTime - currentTime;
	cout << "the difference computed is " << time_difference;
	return time_difference;
}
I have since altered the program to include flags for 12am/12pm which has solved the problem but now the program saves the first current time and refers to that for all future jumps for some reason instead of updating the time. Here is my code thus far.

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am);
void print_future();
void print_past();
void print_SecurityProtocol();
int system_time(int c_hrs, int c_mins, bool c_am, int system_Store);
int main()
{
	int c_hrs, c_mins;
	int t_hrs, t_mins;
	int system_time2;
	int time_difference2;
	bool c_am = 0;
	bool t_am = 0;
	char c, c2, Y, N, y, n, jumpAgain;
	string am_or_pm_current, am_or_pm_target;
	bool g_stop = true;
	cout << "Welcome to Po Sled" << endl;
	cout << "\tSystem booting..." << endl;
	for (int i = 0; i<1; i++) //for loop to run once
	{
		cout << "\n\tEnter current time below: " << endl;
		cout << "\t>Enter hour: "; //User inputs current time in hours
		cin >> c_hrs;
		while (c_hrs > 12 || c_hrs < 1)
		{
			cout << "Error: Please enter an hour in the range [1, 12]: ";
			cin >> c_hrs;
		}
		cout << "\t>Enter minutes: "; //User inputs current time in minutes
		cin >> c_mins;
		while (c_mins > 59 || c_mins < 0) {
			cout << "Error: Please enter minutes in the range [0, 59]: ";
			cin >> c_mins;
		}
		cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM
		cin >> am_or_pm_current;
		while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") { //Checks if valid input, if not repeats message until valid
			cout << "\tError: Please enter AM/am or PM/pm: ";
			cin >> am_or_pm_current;
		}
		if ((am_or_pm_current == "am") || (am_or_pm_current == "AM"))
		{
			c_am = 1;
		}
		else if ((am_or_pm_current == "pm") || (am_or_pm_current == "PM"))
		{
			c_am = 0;
		}
		cout << "\n\tCurrent system time set to " << c_hrs << ":" << setw(2) << setfill('0') << c_mins << am_or_pm_current << endl;
		cout << "\n\t\tIs this time correct (Y or N)? ";
		cin >> c;
		while (c != 'Y' && c != 'y' && c != 'n' && c != 'N')
		{
			cout << "\t\t\tError: Please enter Y/y or N/n: ";
			cin >> c;
		}
		if (c == 'N' || c == 'n')
		{
			continue;
		}
		else
		{
			cout << "\n\tSystem initializing and TARDIS unit warming...." << endl;
			cout << "\tThe Po Sled is engaged and ready for input." << endl;
		}
	}
	do {
			//Starts a loop for target jump
			cout << "\n\tEnter target time below: " << endl; //Enters target time of jump
			cout << "\t>Enter Hour: ";
			cin >> t_hrs;
			while (t_hrs > 12 || t_hrs < 1) {
				cout << "Error: Please enter an hour in the range [1, 12]: ";
				cin >> t_hrs;
			}
			cout << "\t>Enter minutes: ";
			cin >> t_mins;
			while (t_mins > 59 || t_mins < 0) {
				cout << "\tError: Please enter minutes in the range [0, 59]: ";
				cin >> t_mins;
			}
			cout << "\n\tIs it AM or PM?: ";
			cin >> am_or_pm_target; //Classifying if target time is AM or PM
			while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM")
			{ //Validates input is AM or PM
				cout << "\tError: Please enter AM/am or PM/pm: ";
				cin >> am_or_pm_target;
			}
			if ((am_or_pm_target == "am") || (am_or_pm_target == "AM")) 
			{
				t_am = 1;
			}
			else if ((am_or_pm_target == "pm") || (am_or_pm_target == "PM")) 
			{
				t_am = 0;
			}
			cout << "\tTarget time set to " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target; //Sets target time
			cout << "\n\t\tIs this time correct (Y or N)?  "; //Validates if target time entered is correct
			
			cin >> c2;
			while (c2 != 'Y' && c2 != 'y' && c2 != 'n' && c2 != 'N')
			{
				cout << "\t\t\tError: Please enter Y/y or N/n: ";
				cin >> c2;
			}
			time_difference2 = compute_time_difference(c_hrs, c_mins, c_am, t_hrs, t_mins, t_am);

			if (time_difference2 > 360) //If time difference is greater than 6 hours prints error function
			{
				print_SecurityProtocol();
				continue;
			}
			if (c2 == 'N' || c2 == 'n')
			{
				continue;
			}
			cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl;
		 
			if (time_difference2 < 0 && time_difference2 > -360) //If time difference is less than 0 prints past function
			{
				print_past();
			}
			else if (time_difference2 >= 0 && time_difference2 <= 360) //If time difference is ahead of current time prints future function
			{
				print_future();
			}
			cout << "\tWould you like to jump again (Y/N)?  ";
			cin >> jumpAgain;
			while (jumpAgain != 'Y' && jumpAgain != 'y' && jumpAgain != 'n' && jumpAgain != 'N') //Input validation
			{
				cout << "\t\t\tError: Please enter Y/y or N/n: ";
				cin >> jumpAgain;
			}
			if (jumpAgain == 'n' || jumpAgain == 'N') //User exiting program
			{
				if (time_difference2 < 0)
				{
					cout << "\t\tSystem shutting down; enjoy the past.\n" << endl;
				}
				else if (time_difference2 >= 0 && time_difference2 < 360)
				{
					cout << "\t\tSystem shutting down; enjoy the future.\n" << endl;
				}
			}
		
	} while (jumpAgain != 'n' && jumpAgain != 'N');
	return 0;
}

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am) //Computes time differences.
{
	int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime;
	int time_difference;
	if (c_am == 1)
	{
		if (c_hrs == 12)
		{
			currentTime_hours = c_hrs * 0;
		}
		else if (c_hrs != 12)
		{
			currentTime_hours = (c_hrs * 60);
			currentTime_minutes = c_mins;
			currentTime = currentTime_hours + currentTime_minutes;
		}
	}
	else if (c_am == 0)
	{
		if (currentTime_hours == 12)
		{
			c_hrs = c_hrs * 60;
		}
		else if (currentTime_hours != 12)
		{
			currentTime_hours = ((c_hrs + 12) * 60);
			currentTime_minutes = c_mins;
			currentTime = currentTime_hours + currentTime_minutes;
		}
	}
	if (t_am == 1)
	{
		if (targetTime_hours == 12) //if target hours equal to 12 special math
		{
			targetTime_hours = t_hrs*0;
		}
		else if (targetTime_hours != 12) //else do this math
		{
			targetTime_hours = ((t_hrs) * 60);
			targetTime_minutes = t_mins;
			targetTime = targetTime_hours + targetTime_minutes;
		}
	}
	else if (t_am == 0) //if target time equal to pm then do this math
	{
		if (targetTime_hours == 12)
		{
			targetTime_hours = t_hrs * 60;
		}
		else if (targetTime_hours != 12) //else if target time not equal to 12 then do normal pm math
		{
			targetTime_hours = ((t_hrs + 12) * 60);
			targetTime_minutes = t_mins;
			targetTime = targetTime_hours + targetTime_minutes;
		}
	}
	time_difference = targetTime - currentTime;
	cout << "the difference computed is " << time_difference;
	return time_difference;
}

void print_SecurityProtocol() //Function which prints security protocol error message
{
	cout << "\tSecurity Protocols Engaging" << endl;
	cout << "\t\tError: The time difference is greater than 6 hours." << endl;
	cout << "\t\t\tPlease re-enter target time." << endl;
}
void print_past() //Function that prints when a user is in the past
{
	cout << "\tHold onto your lower posterior regions" << endl;
	cout << "\n\t\tYou are now in the relative past" << endl;
}
void print_future() //Function that prints when a user is in the future
{
	cout << "\tHold onto your lower posterior regions" << endl;
	cout << "\n\t\tYou are now in the relative future " << endl;
}
int system_time(int c_hrs, int c_mins, bool c_am, int system_Store)
{ //Function that stores the system time innately to be referenced 
	int system_Time_Hours, system_Time_Minutes;
	if (c_am == 1)
	{
		system_Time_Hours = (c_hrs * 60);
		system_Time_Minutes = c_mins;
		system_Store = system_Time_Hours + system_Time_Minutes;
	}
	else if (c_am == 0)
	{
		system_Time_Hours = ((c_hrs + 12) * 60);
		system_Time_Minutes = c_mins;
		system_Store = system_Time_Hours + system_Time_Minutes;
	}
	return system_Store;
}
Any time you find yourself writing very similar code more than once, you should think about ways to turn it into a function. For example, You could create a function that converts hours, minutes, and am/pm to the number of minutes since midnight:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Convert 12-hour time into the minutes from the beginning of the day.
// hrs must be in the range 1-12. Minutes is 0-59.  Noon is 12:00 PM,
// Midnight is 12:00 AM
unsigned timeToMinutes(unsigned hrs, unsigned minutes, bool isAm)
{
    if (hrs == 12) {            // convert hrs to 0-11
        hrs = 0;
    }
    if (!isAm) {                // convert to 24 hrs clock
        hrs += 12;
    }
    return 60*hrs + minutes;
}

Once you have this, computing the difference between two times is easy. This code will do it. Note that this assumes that if you ask for the difference between, say 3pm and 8am, you mean the 3pm and 8am the next day:
1
2
3
4
5
6
7
8
9
10
11
12
13
//Computes time differences.
int compute_time_difference(int c_hrs, int c_mins, bool c_am,
                            int t_hrs, int t_mins, bool t_am)
{
    unsigned ctime = timeToMinutes(c_hrs, c_mins, c_am);
    unsigned ttime = timeToMinutes(t_hrs, t_mins, t_am);

    // If ttime is the before ctime then it represents the next day
    if (ttime < ctime) {
        ttime += 60*24;
    }
    return ttime - ctime;
}


Topic archived. No new replies allowed.