Topical Information

This program will let you demonstrate your skills at developing an application using looping and branching (and let's not forget our old friend integer arithmetic!). (There are also opportunities for using functional design down in the options.)

Program Information

Write a program that tells the user what time of day it is in both 24-hour (aka military) & AM-PM formats.

What? Not 'fun' enough? Okay, then, to make it more interesting, also tell them:

To make these other parts (at least) easier, you'll need to know what the user thinks is the current date as well (month, day, and year — read this in a standard format).

Be sure to report the results neatly.

Hints:

Time of Day:
Date:

People keep wanting to call the functions localtime and asctime from the standard C library ctime here. Just don't! The goal of this program is for you to code this process yourself! (And asctime leaves a mess behind you would probably forget to clean up even if you knew how!)


As an example, you might have the program interaction look something like (the parts colored like this were typed by the user):

$ ./time.out

                 Welcome to the Time-of-Day Program!!!

What's today?  10/7/2002

Ah, then it is currently 12:59:59 (12:59:59 P.M.).

BTW, there have been 280 days so far this year.  And it's been
32 years since 1970!  Did you know that there have been 8 leap
days in that time?

Thank you for using the ToDP!!

Endeavor to have a timely day!

$ ./time.out

                 Welcome to the Time-of-Day Program!!!

What's today?  2/7/2005

It is currently 13:00:09 (1:00:09 P.M.).

BTW, there have been 38 days so far this year.  And it's been
35 years since 1970!  Did you know that there have been 9 leap
days in that time?

Thank you for using the ToDP!!

Endeavor to have an extemporaneous day!

$

(Note: Notice that the second time is all nicely formatted — as was the 1st.)

(Note: You may notice during your testing that the times are several hours off from our local time. You didn't necessarily make a mistake! Recall that the time() function returns its hideously large number of seconds with respect to UTC (old GMT) time — in Greenwich, England. Adjusting for this can be a tiny bit tricky — especially when there is a date change (*ahem* ...midnight... *nudge* Oh... So sorry... Didn't see you there...) involved, too. You can leave it alone or check out the options below...)

Thought Provoking Questions

  1. If you had a large number of inches (say 103054), how would you determine how many feet and inches this represents — disregarding whole yards and miles! — ...but without going through any intermediate calculations to determine miles and yards!

    (Hint: There is 1 foot and 10 inches left. For further details of the disturbing English system of units, check this site. Quite illuminating!)

  2. How does this relate to having a huge number of seconds and trying to calculate hours, minutes, and seconds without having to remove decades, years, months, weeks, and days first?

  3. How can you determine if one value is divisible by another? (Hint: think about what it means to be divisible. Since 10 is divisible by 5, 5 divides into 10 with a quotient of 2 and a remainder of 0. But since 11 isn't divisible by 5, 5 divides into 11 with a quotient of 2 and a remainder of 1.)

  4. How can you determine if a year is a leap year? (Don't just re-write what was told to you above — translate it into some C++ logic with tests for divisibility and !, ¦¦, and/or &&!)

    (And no, you can't simply calculate how many years were leap since the epoch! *phbbt*)

  5. Concerning printing all pretty:

    1. How will you decide if a value needs to be printed with a leading 0? Which values are they?

    2. If you don't need to print a leading 0, is there anything else to do? I.E., is there an alternative action?

    3. Do you really need to make any decisions here at all? I.E. is there another way to just have a number automatically print with a leading 0 when it needs to have one? ...I.E. can we get cout to fill in a '0' for us whenever a number is shorter than a set width? ...*shrug*

  6. Is noon A.M. or P.M.? Then which is midnight?

  7. Is midnight 24:00 or 00:00?

  8. How will you decide if a time is before noon?

    What action(s) will you perform if a time is before noon? (Is this so for all formats of such times?)

    If a time is after noon, what action(s) will be taken? (Is this so for all formats of such times?)

  9. When printing the time, what value(s) get printed first no matter if the time is before or after noon?

    When printing the time, what value(s) get printed last no matter if the time is before or after noon?

    HINT:
    Think carefully about these two...look at an A.M. example and a P.M. example and compare the two for similarities. Take, say, 1:03:12 in both the morning and afternoon and line them up... say, one atop the other...*shrug*

  10. How can you have your program print different good-bye message each time it is run? (Didn't notice that part of the sample, did you? Look again... *son-of-a-gun*) (Hint: Think of it as a randomly selected/decided message...)

Be sure to look for any helpful resources you can in the notes on programming with ctime's time() function. You can never tell when something might come in handy!

This assignment is (Level 3.5).

Options