Lab 4 - Apr 18

  1. Write a void function that takes two integer arguments: hours (valid values 0-23) and minutes (valid values 0-59). The function should first check that the value for each argument is within the valid range and print an error message if either is not. The function should print out the time in both 12h format and 24h format. For 12h format, AM is the hours 0 to 11 with the 0th hour being 12am and PM is the hours 12-23 with the hours adjusted to print 12pm to 11pm instead of 12 to 23. You will need to do some calculations within the void function to convert from 24h format to 12h format. Also, to fully emulate a clock, any minutes or hour less than 10 needs to be padded with 0 to two digits. For example, 5 minutes after the hour of 9 is written 09:05. Use cout with if statements to do this. Within main(), ask the user to input the hour and minute in 24h format then call your void function with these values.

    Example of running the final program:

    Enter the hour in 24h format (0-23): 0
    Enter the minute (0-59): 5
    The time in 12h format is 12:05AM.
    The time in 24h format is 00:05.
    
  2. As mentioned in lecture, printf() allows one to do output more succinctly than with using cout. Copy your program from question 1 and add the line
    #include <stdio.h>
    after the iosteam include to gain access to printf(). (Note: in class I left off the .h but Helios's compiler doesn't like this for this file. When in doubt, put the .h on the included filename). printf() has a special formatting string that will add the 0 padding to hours or minutes less than 10 automatically. That formatting string is %0<col>d for integers. For example, for the clock digit, the column width is 2, so the formatting string would be %02d. The "0" flag tells printf() to pad with 0s so if the clock digit is less than 10, it will display 0x but if the digit is 10 or higher, it will display just the digit. Rewrite your function to use printf() with this formatting string instead.
Email all files to my Helios account.