943,997 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4694
  • C++ RSS
Apr 23rd, 2005
0

parking fee

Expand Post »
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 ..

C++ Syntax (Toggle Plain Text)
  1. cout << "Time entered? ";
  2. cin >> enter;
  3. cout << "Time departed? ";
  4. cin >> exit;
  5.  
  6. minutesEnter = enter % 60;
  7. hoursEnter = enter / 60;
  8.  
  9. minutesExit = exit + 60;
  10. hoursExit = exit - 1;
  11.  
  12. cout << fixed;
  13. cout.precision(2);
  14. cout << setfill('0');
  15. cout << "Time in: " << hoursEnter << ":" << setw(2) << minutesEnter << endl;
  16. 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 =)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dazzer143 is offline Offline
1 posts
since Apr 2005
Apr 27th, 2005
0

Re: parking fee

Quote originally posted by dazzer143 ...

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!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Pikachu is offline Offline
19 posts
since Aug 2004
Apr 27th, 2005
0

Re: parking fee

Other way is get everything on minutes

C++ Syntax (Toggle Plain Text)
  1. enterMin=enter%100; //enterMin is int
  2. enterHour=enter/100; //enterHour is int
  3. enterTime=(eneterHour * 60) + enterMin; //which give you enter time in minutes

SAME apply to live time
than small if statement which check
C++ Syntax (Toggle Plain Text)
  1. if (enterTime < leaveTime)
  2. {
  3. c=leaveTime - enterTime; //total parking time in minutes
  4. b=c/60; //Hours of parking
  5. a=c%60; //minutes of parking
  6. }
  7. else if (enterTime > leaveTime)
  8. {
  9. d=1440-enterTime; //1440 is 24hour in minutes and you get time till midnight
  10. c=d+leaveTime; //total parking time
  11. b=c/60; //Hours of parking
  12. a=c%60; //minutes of parking
  13. }
  14. else
  15. {
  16. SOME MESSAGE SAYING THAT PARKING TIME IS EXACTLY 24HOURS OR NULL
  17. }
<< moderator edit: added [code][/code] tags >>
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Controlling Hardware
Next Thread in C++ Forum Timeline: What's wrong with string.replace()?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC