View Single Post
Oct26-10, 06:51 PM
P: 21,311
Comp sci program about time

Quote Quote by burns12 View Post
That does help actually, it makes sense to do it like that now that I see it. But the problem is we haven't done any of that in class. I have some programming experience in java and this is c++ so I know the basics but this is killing me. We haven't done loops yet even. All we have to work with is functions and if/else statements.
For what you need to do, you don't need loops. You are taking a starting time and a quitting time and calculating the number of hours and the pay - that's all.
Quote Quote by burns12 View Post

How do you get it to convert the time to 24 hour time?
You need to do it in two parts: the hour and the minutes.
If the hour is earlier than 1 PM, multiply by 100. For example 8 AM is 800.
If the hour is 1 PM or later, add 2. For example, 1:00 PM is 1300.

After you have converted the hours to 24-hour time, add on the minutes. For example: 8:30 AM becomes 830 and 2:15 PM becomes 1415.

This leads to some difficulties, though, because if someone starts work at 8:30 (830) and works to 2:15 (1415), you might naively calculate the elapsed time as 1415 - 830 = 585, which is incorrect. The actual amount of time is 5 hours and 45 minutes.

To handle this difficulty it might be simpler to convert to hours + decimal part. Using the same times,
8:30 = 8.5
2:15 = 14.25

14.25 - 8.5 = 5.75, which is 5 hours and 45 minutes, the correct elapsed time.

To summarize, after parsing the input string into hour and minutes, convert any times after 1 pm by adding 2, and convert minutes to fractions of hours by dividing the minutes by 60.

For example 4:55 PM = 16.916667 (rounded).

Quote Quote by burns12 View Post
What type should I make the input variables? Ints or strings or what?
Making the input times strings would make for simpler user input, but more complicated code, since you have to take the string apart and extract the information in it.