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.)
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:
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...)
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!)
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?
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.)
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*)
Concerning printing all pretty:
How will you decide if a value needs to be printed with a leading 0? Which values are they?
If you don't need to print a leading 0, is there anything else to do? I.E., is there an alternative action?
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*
Is noon A.M. or P.M.? Then which is midnight?
Is midnight 24:00 or 00:00?
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?)
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*
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).
Add (Level 2) to re-factor your program to use at least these eight functions:
Add (Level 1.5) to place all of your time and date functions into a handy library for easy reuse. (Hint: Don't name your library time since that is already taken. *grin* ...remember that ctime is known as time.h when used in C...)
Add (Level 1) to make the entry of the year more natural for the user. Allow them to type in a 1 or two-digit year. Make a reasonable decision as to whether it should be a 1900 or 2000 year. (Hint: does this decision have anything to do with the 1970 year from the time() function? Or does it have more to do with the years people normally deal with — which tend to be further into the past than into the future?)
Add (Level 1) to adjust for GMT (Greenwich Mean Time) back to US Central. This adjustment should be made relative to the date given being the correct local date. You might look at this page for some GMT vs. US Central ideas... (Oh, and watch out... Daylight Savings Time can really mess with your program!)
If it is 04:00 in Greenwich, England, what time is it here in Chicago?
If it were 10/7/2002 in Greenwich, England, what date is it here in Chicago? (Is there just a single answer?)
Add (Level 1.5) to give the user a menu of time zones to include at LEAST Eastern, Central, and Pacific US. Times should be adjusted to the user's choice of time zone — not forced to US Central. (This option assumes you are doing the GMT adjustment option above. They will total to (Level 2.5) if you do both this menu and the actual adjustment(s).) (Hint: If done well, there will just be one adjustment code and a few con... Oops... I think I've said too much already...)
Add (Level 3) to also calculate and print out the current date (day, month, and year). Since they've already entered the 'date', tell them whether they were correct or not. If not, give the date 'stats' for the actual date as well as the one they entered. (This does re-use some of the tools we've seen/built before, but some in a different way. It is a bit tedious and can be tricky!)
If you've already calculated the number of seconds since this just past midnight, how hard can it be to calculate the number of seconds which led up to then?
Although years aren't all even (darn 'leap' crap), every group of four years has 3*365 + 366 = 4*365 + 1 days in it... And you already know how many seconds are in a day... Hmm...
The months are even less 'even', of course, but that's what branches and constants are for, right?
Add (Level 1.5) to make good use of a switch for adding up the days so far this year. (Hint: Does the leap day of a leap year count in February or thereafter..?)
Add (Level 1) to use a switch for the time zone menu. (Yes, that would make for a total of (Level 3.5) for the menu option.)
Add (Level 1.5) to allow the user to enter the date in any of the following formats:
(Hint: Remember to look at what you are reading before you read it!)
(Hint II: For the last two, you could use .substr() (possibly in conjunction with .length()) to decide if the user's input matched the actual month name (be it a 3-letter abbreviation or the whole name). Oh, and did I mention that recognizing both these formats with a single piece of code is worth an extra (Level 1)?)
You can add another (Level 0.5) to ignore case during the decision for the last two formats... You can even increase this by (Level 1) to re-use the function you wrote for that other lab!
Also, if you are doing the calculate the date option above, display the true date in the same format the user used to enter their date. But always use proper case when you print your date — whether they were sloppy with case or not.
And add another (Level 4) to generalize the last two date entry format checks with a vector of month names and a linear search through it. (Yes, you can get all of these levels at the same time! That's perfectly fine!)