//A c++ program that calculate and print parking charges
#include <iostream>
using namespace std;
#include <iomanip>

const double minimumFee       = 7.00;
const double hourlyFee    = 1.75;
const int    numOfCustomers   = 12;
const int    numOfMinsPerHour = 60;

void inputAndValidate(int&,int&,int&,int&);
void convertToMinutes(int&,int&,int&,int&);
void convertBackToTime(int&,int&,int&);
double calcCharge(int,int);

int entranceMinutes,exitMinutes,entranceTimeInMins,exitTimeInMins ;
int minutesParked,parkedMinutes,entranceHour,exitHour,parkedTime ;
int parkedHours,numb;
int count =1;

int main()
	for (int num = 1; num <= numOfCustomers; num++ )
		while(count <=12){

		return 0;
void inputAndValidate (int& entranceHour,int& entranceMinutes,int& exitHour,int& exitMinutes)
	int invalid =1 ;
	while(invalid ==1){
	cout<<"\nEnter entrance time in hours and minutes\n";
	cin>>entranceHour>>entranceMinutes ;
	cout<<"\nEnter exit time in hours and minutes\n";
	if(entranceHour == 24)
		entranceMinutes = 00;
	if(exitHour ==24)
		exitMinutes = 00;
	if(entranceHour<0 || entranceHour >25 ||entranceMinutes<0 || entranceMinutes>=59 ||
		exitHour<0 || exitHour>25 || exitMinutes <0 || exitMinutes>=59 ){
		cout<<"Invalid time.Try again:\n";
		invalid-- ;
	  count --;
void convertToMinutes(int& entranceHour,int& entranceMinutes,int& exitHour,int& exitMinutes)
	entranceTimeInMins = (entranceHour * numOfMinsPerHour + entranceMinutes);
	exitTimeInMins = (exitHour * numOfMinsPerHour + exitMinutes);
	minutesParked = (exitTimeInMins - entranceTimeInMins);
void convertBackToTime(int& minutesParked,int& parkedHours,int& parkedMinutes)
	parkedHours = (minutesParked / numOfMinsPerHour);
    parkedMinutes = (minutesParked % numOfMinsPerHour);
double calcCharge(int parkedHours ,int parkedMinutes)
	double charge;
	if (parkedHours>=3 && parkedHours<=24 && parkedMinutes>0 && parkedMinutes<=59)
		int additionalHourMin = (parkedHours - 2);
		charge = (additionalHourMin * hourlyFee + minimumFee);
	    return charge;}
		if(parkedHours>=3 && parkedHours<=24 && parkedMinutes == 0){
			int additionalHours = (parkedHours - 3);
			charge = (additionalHours * hourlyFee + minimumFee);
			return charge;}
			if(parkedHours>0 && parkedHours<3 && parkedMinutes>=0 && parkedMinutes<=59){
				charge = minimumFee;
				return charge;}
		return 0;

I think my calculation are correct according to problem statement"A parking garage charges a R7.00 minimum fee to park for up to three hours. The garage charges an additional R1.75 per hour or part thereof in excess of three hours".I misunderstand the results.

Recommended Answers

All 3 Replies

Your problem is one or more of the following going wrong.

  1. Input doesn't work.
  2. Calculation doesn't work.
  3. Parameter passing doesn't work.
  4. Something else.

Figure out which one it (or more) it is. Then tackle it. A good start would be to add a few debug lines at the very beginning of the calcCharge function printing out the parameters passed to the function. Do the same for the END of the function printing out what it returns. If the parameters print right and the rerurn prints wrong, it's # 2. If they BOTH print right, it's NOT # 2. If the parameters don't print right, then it's #1 or #3 or #4, so again add some debugging statements at the top printing out the parameters for all the functions and at the bottom returning from them. Eventually you'll narrow down a bad function or confirm that they all work. IF they all work, then it's parameter passing or something else. Once you narrow it down, then keep narrowing it down by adding more and more debugging statements till they first display incorrectly. At that point you will have found the exact problem.

Your calculator seems to be extremely complex for the task as stated.

if minutes > 0 then hours++    ; If "part thereof", add 1 to hours
subtract minhours from hours   ; calculate hours over minimum
charge = mincharge + (hours * hourlycharge)

It is also generally considered "a bad thing" to use global variables, as you have declared in lines 16-19. Instead you should pass the ones you need to each function: by reference for "output" values that the function should alter, and by copy (or const reference) for "input" values. Conversely, if you're going to make all the variable global anyway, you don't need to pass any of them into your functions.

Is there a reason you loop over your input/convertToMinutes/convertBack/output 12 times for the first customer? If you don't reset count, you won't loop at all for your next customer(s).

Your upper-limit validation for hours and minutes are incorrect in opposite directions: 25 is not a valid hour (but will pass), 59 is a valid number of minutes (but will fail). What happens if the customer enters at 23:30 Friday night, and leaves at 1:30 Saturday morning?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.