Returning to the original question, the program needs to do the following:
- Input the entry and exit time.
- Compute the number of hours that elapsed.
- Compute the parking fee from the number of elapsed hours.
- Print the parking fee.
The trickiest part is computing the elapsed hours. First a question: If someone parks for 2 hours and 12 minutes, does that count as 2 hours or 3? Get clarification from the professor. In most real world parking lots, it counts as 3 hours: the time rounds up.
Entry Exit Elapsed hours
08:12 08:13 1
08:12 09:09 1
09:09 08:12 24 |
I suggest you get the number of hours like this:
- Convert hrs and minutes to total elapsed
minutes.
- If the elapsed minutes for the exit time is less than that of the entry time, then the car left the day after it arrived. Add 24*60 minutes to the exit time. In this way, both elapsed times reflect the elapsed minutes from the beginning of the day when the car entered.
Subtract exit-entry to get the total elapsed minutes that the car was in the lot.
elapsed hours is elapsed minutes / 60, but there's a catch:
that will round down and you need to round up. There's a handy trick to rounding up, just add 59 to the elapsed minutes. So
rounded_up_hours = (elapsed_minutes+59) / 60;
Okay. Here's how you should write the program. There are lots of steps here but most of them are small incremental changes.
1. Write a program that prompts the user for the time in 24 hr format. Question: does that mean 15:30 or 1530? Whatever it is, have the program print out the hours and minutes separately. Note that we'll be removing this printout code eventually, it's just here for debugging.
Assuming 15:30 format:
Input:
08:21
Output:
8 21 |
2. Modify the program to prompt for and record the entry time and exit time to the lot. Print out the hours and minutes of both. If you've learning about functions, you might find that a function to read hours and minutes is handy. Maybe something like
void getHrsAndMins(int &hours, int &minutes);
3. Write a function that converts hours and minutes to the total elapsed minutes. Convert both entry and exit time to total elapsed minutes and print the result. Verify by hand that you've got the numbers right.
4. Add the code to compute the rounded up hours:
- Add 24*60 minutes to the exit minutes if the exit < entry
- Compute the total elapsed minutes
- Compute the rounded up hours that the car was in the lot.
Print each of these as you go and verify by hand.
5. Compute the parking fee from the rounded up hours. Print it out.
6 Try some test cases. Do at least the three that I included above.
7. Remove all the debugging output so it just prints the total parking fee.
When you get stuck for more than an hour, post your code, along with any error messages if it won't compile, or the exact input and output if it's giving bad results. Ask specific questions.