i'm having a trouble getting this right ..

Write a program to calculate parking fee for customers who park their vehicles in a parking lot when the following information is given:

The time the vehicle entered the lot (in 24-hour format, without a colon, such as 1645)
The time the vehicle left the lot, also in 24-hour format without a colon.

To calculate the actual time spent in the parking lot, you may use this algorithm (it's not the only way to do it):

Compare the minute portion of the leaving and entering time. If the first one is smaller than the second,
Add 60 to the minute portion of the leaving time.
Subtract 1 from the hour portion of the leaving time.
Subtract the hour portions.
Subtract the minute portions

was try using the split hour to calculate the hourly and minutes but it didnot come out right ..

cout << "Time entered? ";
	cin >> enter;
	cout << "Time departed? ";
	cin >> exit; 

    minutesEnter = enter % 60;
    hoursEnter = enter / 60;

    minutesExit = exit + 60;
    hoursExit = exit - 1;    

    cout << fixed;
    cout.precision(2);
    cout << setfill('0'); 
    cout << "Time in: " << hoursEnter << ":" << setw(2) << minutesEnter << endl;
    cout << "Time out: " << hoursExit << ":" << setw(2) << minutesExit << endl;

<< moderator edit: added [code][/code] tags >>

i'm sort of new to this .. please help me out ..

thank you =)


Compare the minute portion of the leaving and entering time. If the first one is smaller than the second,
Add 60 to the minute portion of the leaving time.
Subtract 1 from the hour portion of the leaving time.
Subtract the hour portions.
Subtract the minute portions

minutesEnter = enter % 60;
hoursEnter = enter / 60;

minutesExit = exit + 60;
hoursExit = exit - 1;

Basically, what you had in mind and what the computers is 'thinking' is totally different. When we read time in 24-hours format (ie 1645) we know it means 4.45pm. But the computer only take it as a normal number (one thousand six hundred forty five).
Bare ONE thing in mind. This is normal MATHEMATICS for the computer and not TIME!

So, what you should do is very simple.

Compare the minutes of both times by using modulus 100.
If exit minute is smaller than entry, add 60 to exit time and minus 100 also to exit time. Then you minus them like a normal math sum.
Else, you just minus them like any other normal math sum.

Take example enter time is 1645 while exit time is 1715.

Why I use modulus is that I only want the minutes and not the hours.
So, when I modulus 1645 with 100 I get 16 extra 45. Right?
And moduls 1715 with 100 I get 17 extra 15. Right?
Then I compare 15 and 45. (obviously 45 is bigger)
So, I add 60 to exit. (1715+60=1775)
Then, minus 100 (as in minus 1 hour) from from exit. (1775-100=1675)
So, now exit holds the value of 1675 while enter is still 1645.
Finally, you minus 1675 and 1615 to get 30!

HOWEVER, since you enter 1645 BUT you want a display 16:45 right?
You'll need to have 2 variables. One to hold the modulus 100 result (which would be the minutesEnter / minutesExit) and another to hold the sum of enter/exit divided by 100 (which would be the hoursEnter / hoursExit).

Hope this helps. Good luck!

Other way is get everything on minutes

enterMin=enter%100;                              //enterMin is int
enterHour=enter/100;                             //enterHour is int
enterTime=(eneterHour * 60) + enterMin;   //which give you enter time in minutes

SAME apply to live time
than small if statement which check

if (enterTime < leaveTime)
{
c=leaveTime - enterTime;                        //total parking time in minutes
b=c/60;                                                //Hours of parking
a=c%60;                                               //minutes of parking
}
else if (enterTime > leaveTime)
{
d=1440-enterTime;                                 //1440 is 24hour in minutes and you get time till midnight
c=d+leaveTime;                                     //total parking time
b=c/60;                                                //Hours of parking
a=c%60;                                               //minutes of parking
}
else
{
SOME MESSAGE SAYING THAT PARKING TIME IS EXACTLY 24HOURS OR NULL
}

<< moderator edit: added [code][/code] tags >>

This article has been dead for over six months. Start a new discussion instead.