Topical Information

This program gives you practice with branching (ifs) and looping (whiles/indefinite), function testing (i.e. writing a driver), and input stream magic (see below).

Program Information

Write a function which allows the user to type in time values in either military (24-hour) format or AM/PM format.

Note: If the hours they enter are 13 or higher or 0, you know it is military time. But if their hours are 1-12, you aren't sure.

You'll probably find the peek() function useful here. (Recall that it takes no arguments and returns the next character from the input stream (without removing it from the stream -- as get() or >> would do. For example, if the user had typed 'hello', the following:

   ch = cin.peek();

would store 'h' in ch and the input stream would still contain 'hello'. Whereas if it had been ch=cin.get(); or cin>>ch;, ch would have still had an 'h' in it but the stream would only hold 'ello'!)

If the user enters something that cannot be read as a proper time value, don't alter the caller's variables and set/let cin [to] have a failed state. That way the calling part of the program can use normal fail-detection techniques to notice that your function failed to read -- just like it uses to detect when a pure extraction fails to read.

Once you feel this is ready (i.e. you've tested it by hand), write a simple driver to call the function and print its output on the screen for verification.


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

$ ./timedriv.out

                 Welcome to the Time Driver Program!!!

Enter time:  13:30

You've entered 1:30 in the afternoon!

Another time?  y

Enter time:  1:30 pm

You've entered 1:30 in the afternoon!

Another time?  yes

Enter time:  24:30

I'm sorry, that is not a valid time!  Please use a clock next time!

Another time?  yeah

Enter time:  14:30 am

I'm sorry, that is not a valid time!  Please use a clock next time!

Another time?  NO!!!


Thank you for using the TDP!!

Endeavor to have a lugubrious day!

$

Thought Provoking Questions

  1. For some times, you can be certain whether it is morning or afternoon. Which are these?
  2. When you cannot be sure based on the hours alone, what clue can you rely on to tell you if it is a morning or afternoon time?
  3. How do you tell when you are at the end of the input line? (Hint: what character ends any input line?)
  4. When do you need to use the peek function here?
  5. Hmm...when there's spacing (i.e. ' ', '\t', etc.) in the stream, what will peek do? Is there some way to get around -- or past -- this problem?
  6. Can your program handle the user typing "A.M." or "P.M.", too (i.e. capitalized &/or with dots)? (It should be able to...)
  7. Speaking of "?.m.", after you've looked at what you want, what do you do with the rest of the user's input line, anyway? (Hint: You probably shouldn't leave it there, should you?)
  8. What is a driver, anyway? (Use your OWN words!!!)
  9. What kind of loop did you use for your driver? What is your driver's condition? (Explain any variables and extra initialization that might be important here but not obvious without the whole code.)
  10. Can your driver handle both upper- and lower- case entries for it's condition? What library function might help you here?
  11. Can your driver handle both 'y' and 'yes' for the user's response to it's question? What library function (& constant) might help you here?
  12. How many branches does your time input function have? How many tests will be required to thoroughly test it? (Isn't that loop in the driver handy when you have so many tests?)

    (This last question is rhetorical, btw.)
  13. Technically, the loop's condition adds to the required testing for your program. How many new tests are added by your driver's condition? Can these be done during a single run of the program? How many runs will be required? (Perhaps the loop hasn't removed all the multiple program executions, after all...)

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).

Options

 

More options coming soon..?