Time Travel program not working properly

Hello I'm writing a program which calculates the difference between two inputted times, current and target, and within conditions allows the jump or does not. I have done this by converting the time differences in a function that computes each time in minutes and tests whether the jump is too large (over 6 hours), or in the past and future. However, it's
1) ignoring the time constraints in certain cases for some reason (usually on the first "jump" but also on other ones, and I believe it has something to do with the boolean variables responsible for setting am or pm to true/false
2) printing out the past message even if the user is in the future and vice versa.

I've spent a long time trying to debug this and I can't wrap my head around the reasons for this behavior

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
#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 t_hrs, int t_mins, bool t_am, int system_time);
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;

			
			cout << "\n\t\tIs this time correct (Y or N)?  ";
			
			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;
			}
			else if (time_difference2 < 0) //If time difference is less than 0 prints past function
			{
				print_past();
			}
			else if (time_difference2 >= 0) //If time difference is ahead of current time prints future function
			{
				print_future();
			}
			cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl;
			cout << "\tWould you like to jump again (Y/N)?  ";
			cin >> jumpAgain;
			if (jumpAgain == 'n' || jumpAgain == 'N')
			{
				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)
	{
		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 << "time difference computed, " << 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 << "\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 << "\tYou are now in the relative future " << endl;
}
int system_time(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am, int system_time)
{ //Function that stores the system time innately to be referenced 
	int system_Time_Hours, system_Time_Minutes, system_Time;
	if (c_am = 1)
	{
		system_Time_Hours = (c_hrs * 60);
		system_Time_Minutes = c_mins;
		system_Time = system_Time_Hours + system_Time_Minutes;
	}
	else if (c_am = 0)
	{
		system_Time_Hours = ((c_hrs + 12) * 60);
		system_Time_Minutes = c_mins;
		system_Time = system_Time_Hours + system_Time_Minutes;
	}
	return system_time;
}

1
2
3
4
5
6
7
	
if (c_am = 1)
{
    currentTime_hours = (c_hrs*60);
    currentTime_minutes = c_mins;
    currentTime = currentTime_hours + currentTime_minutes;
}

Use 2 equal signs for equality.

Consider using a class/struct to store your time.
Also, break your main function up into functions and use spacing to make it easier to read. At the moment it is just excruciating to read.

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
bool is_valid( const Time& t )
{
    // < 0 in case the user enters 0:00
    // which is a perfectly valid time
    if( t.hour < 0 || t.hour > 12 ) return false;
    if( t.min < 0 || t.min > 59 ) return false;
    return true;
}

struct Time
{
    Time( int h = 0, int m = 0, bool am = false ) : hour{ h }, min{ m }, is_am{ am }
    {
        if( !is_valid ) 
            // error
    }

    int hour, min;
    bool is_am;
};

Time get_time( )
{
    Time t{};
    cout << "\n\tEnter current time below: \n";

    cout << "\t>Enter hour: "; //User inputs current time in hours
    cin >> t.hour;

    cout << "\t>Enter minutes: "; //User inputs current time in minutes
    cin >> t.min;

    if( !is_valid( t ) ) t = get_time( );

    string am_pm{};
    cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM
    cin >> am_pm;
    for( char& c : am_pm ) c = tolower( c );
    while( !(am_pm == "am" || am_pm == "pm") ) {
        cout << "\t>Is it AM or PM?: ";
        cin >> am_pm;

        for( char& c : am_pm ) c = tolower( c );
    }
    t.is_am = ( am_pm == "am" );
    
    // could overload operator<< for Time object
    cout << "\n\tCurrent system time set to " << 
        t.hour << ":" << setw(2) << setfill('0') << t.min << am_pm << "\n";
    cout << "\n\t\tIs this time correct (Y or N)? ";
    cin >> c;
    c = tolower( c );
    while( c != 'y' && c != 'n' ) {
        cout << "\t\t\tError: Please enter Y/y or N/n: ";
        cin >> c;

        c = tolower( c );
    }

    if ( c == 'n') t = get_time( );

    // prefer "\n" rather than endl to avoid unecessary call to flush()
    cout << "\n\tSystem initializing and TARDIS unit warming....\n";
    cout << "\tThe Po Sled is engaged and ready for input.\n";

    return t;
}

int main( )
{
    cout << "Welcome to Po Sled\n";
    cout << "\tSystem booting...\n";

    // much cleaner
    Time t{ get_time( ) };

    // ...
}
Last edited on
Thanks for the reply! I can't use classes/structures just yet because we haven't covered it in my class, but I appreciate the re-formatting you did with the other functions, it will help a lot with keeping my main function cleaner.

I am just a little confused about the purpose of
tolower(c)

What does that do? I haven't covered that in my class or my readings so I'm unsure.
Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged.

http://www.cplusplus.com/reference/cctype/tolower/
Topic archived. No new replies allowed.