parking fee

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 1
Reputation: dazzer143 is an unknown quantity at this point 
Solved Threads: 0
dazzer143 dazzer143 is offline Offline
Newbie Poster

parking fee

 
0
  #1
Apr 23rd, 2005
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 ..

  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 =)
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 19
Reputation: Pikachu is an unknown quantity at this point 
Solved Threads: 0
Pikachu's Avatar
Pikachu Pikachu is offline Offline
Newbie Poster

Re: parking fee

 
0
  #2
Apr 27th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,185
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: parking fee

 
0
  #3
Apr 27th, 2005
Other way is get everything on minutes

  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
  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 >>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC