Parking Deck Simulator

Hey guys! Below is a program that I coded that is meant to simulate a parking meter within a parking deck. The program has no bugs, but the outputs are coming out wrong.

For example, if I input 05:45 to 07:00, it wrongly calculates the time in between as 2 Hours.

And if I input 06:32 to 09:54, it wrongly calculates the time in between as 2.1 Hours.

Is there a way I can get the program to calculate the correct time so I can print out the proper bill amount?

I appreciate you guys so much!

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
/* Purpose:             This program is a Parking Deck Ticketing System. Punch in your arrival time 
			(hours then minutes) and your departure time (hours then minutes). If you lost
			your parking ticket, input the values of 55; if you have a special parking pass, 
			select 99 for all values. Make sure you stay within the range. Enjoy! 
*/

#include <iostream>
#include <iomanip>
using namespace std;
double H_out, H_in, M_out, M_in, Time_passed(0), cost(0);
void getTime (double & H_out, double & H_in, double & M_out, double & M_in)

{
	cout<< "For Special Ticket Holders only, input 99 for arrival hour and arrival minute" << endl;
	cout<< "If ticket has been misplaced, input 55 for departure hour and departure minute:" << endl;
	cout<< "Input arrival time in hours in the 0 - 24 hour range" << endl; 
	cin>>H_in;
	cout<< "Input arrival time in minutes in the 0 - 59 minute range" << endl; 
	cin>>M_in;
	cout<< "Input departure time in hours in the 0 - 24 hour range" << endl; 
	cin>>H_out;
	cout<< "Input departure time in hours in the 0 - 24 hour range" << endl; 
	cin>>M_in;

}

double timePassage()
{
	if (H_out >= H_in)
	Time_passed = (H_out + M_out/60) - (H_in + M_in/60);
	else if (H_out < H_in)
		Time_passed = (24-(H_in + M_in/60)) + (H_out + M_out/60);
	return Time_passed;
}

double PriceCalculation()

{
	if (H_out == 55 && M_out == 55)
		cost = 110.00;
	else if (H_out == 99 && M_out == 99)
		cost = 5.00;
	else if (Time_passed < 0.5)
		cost = 3.00;
	else if (Time_passed < 1)
		cost = 5.00;
	else if (Time_passed < 2)
		cost = 10.00;
	else if (Time_passed < 3)
		cost = 15.00;
	else if (Time_passed < 4)
		cost = 30.00;
	else if (Time_passed >= 4 && Time_passed < 12)
		cost = 30.00 + (Time_passed - 4)*10;
	else if (Time_passed >= 12)
		cout << "Error" << endl;
	else
	{
		cout << "There was an error, your value(s) were incorrect!" << endl;
		cost = 0.00;
	}

return cost;

}

void printoutReciept()

{
	cout << "Your time of arrival is:" << setfill('0') << setw(2) << H_in << ":" << setfill ('0') << setw(2) << M_in << endl;
	cout << "Your time of departure is:" << setfill('0') << setw(2) << H_out << ":" << setfill ('0') << setw(2) << M_out << endl;
	cout << "Your amount of time parked is:" << Time_passed << "Hours" << endl;
	cout << "Your bill is $" << fixed << setprecision(2) << cost << endl;
}

int main()

{
	cout << "This is Paye Kialain's Parking Meter; Enjoy!" << endl;
	getTime (H_out, H_in, M_out, M_in);
	timePassage();
	PriceCalculation();
	printoutReciept();
	cout << "coded by Paye W. Kialain" << endl;
system("PAUSE");
return 0;
}
1
2
3
4
cout<< "Input departure time in hours in the 0 - 24 hour range" << endl; 
cin>>H_out;
cout<< "Input departure time in hours in the 0 - 24 hour range" << endl; 
cin>>M_in;


Shouldn't the last one ask for minutes rather than hours?

Edit: You're using M_In twice, rather than M_out.
Last edited on
Oh yeah, thanks for the heads up, I just fixed that... but the calculation error is still in there. Not sure how to get the right calculations.
Yeh there's a much better way of calculating things. Using modulus.

I'll give you a peace of code, with the input and the calculations. You can then implement it to your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int H_out, H_in, M_out, M_in, Time_passed(0), cost(0), Minute_passed(0);

	cout << "For Special Ticket Holders only, input 99 for arrival hour and arrival minute" << endl;
	cout << "If ticket has been misplaced, input 55 for departure hour and departure minute:" << endl;
	cout << "Input arrival time in hours in the 0 - 24 hour range" << endl;
	cin >> H_in;
	cout << "Input arrival time in minutes in the 0 - 59 minute range" << endl;
	cin >> M_in;
	cout << "Input departure time in hours in the 0 - 24 hour range" << endl;
	cin >> H_out;
	cout << "Input departure time in hours in the 0 - 24 hour range" << endl;
	cin >> M_out;

	Time_passed = (H_out  - H_in + 24) % 24;
	Minute_passed = (M_out - M_in + 60) % 60;

	cout << Time_passed << ":" << Minute_passed << endl;


Thanks so much! That modulus method will certainly work. Should I now change everything from double to int since the modulus doesn't require double values?

And also, the Time_Passage and Price_Calculation functions are coming up with some errors; how should I alter these functions?
Yes change it to int. Also, I dont quite understand the if statement in the price calculation function.

if (H_out == 55 && M_out == 55)

Maybe it had something to do with your previous calculations, but H_out can never be 55? its a 24 hour system.
Topic archived. No new replies allowed.