fork download
  1. // Attached: HW_3f
  2. // ===========================================================
  3. // File: HW_3f.cpp
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <limits>
  12. using namespace std;
  13.  
  14. // ===========================================================
  15. // Global Constants
  16. // ===========================================================
  17. const int MAX_HOURS = 23;
  18. const int MAX_MINS = 59;
  19. const int MAX_SECS = 59;
  20.  
  21. // ===========================================================
  22. // Struct
  23. // ===========================================================
  24. struct Time
  25. {
  26. int hours;
  27. int minutes;
  28. int seconds;
  29. };
  30.  
  31. // Function Prototypes
  32. void getTime(Time &time);
  33. bool isTimeValid(const Time &time);
  34. void addOneSecond(Time &time);
  35. void displayTime(const Time &time);
  36.  
  37. // ==== main ====================================================
  38. // Prompts user for a valid 24-hour time, adds one second,
  39. // displays the result, and allows repetition.
  40. // =============================================================
  41. int main()
  42. {
  43. Time time;
  44. char again = 'n';
  45.  
  46. do
  47. {
  48. getTime(time);
  49. addOneSecond(time);
  50. displayTime(time);
  51.  
  52. cout << "Do it again? (Y/N) ";
  53. cin >> again;
  54. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  55.  
  56. } while (again == 'Y' || again == 'y');
  57.  
  58. return 0;
  59. } // end of main()
  60. // =============================================================
  61.  
  62.  
  63.  
  64. // ==== getTime =================================================
  65. // Gets hours, minutes, and seconds from the user.
  66. // Repeats until valid time is entered.
  67. //
  68. // Input:
  69. // time - (IN & OUT) Time struct passed by reference
  70. //
  71. // Output:
  72. // None
  73. // =============================================================
  74. void getTime(Time &time)
  75. {
  76. bool validInput = false;
  77.  
  78. do
  79. {
  80. cout << "Enter the time in \"military time\", (24-hour format),\n";
  81. cout << "in the following order: HH:MM:SS, (Hours, Minutes, Seconds).\n";
  82.  
  83. cout << "Hours: ";
  84. if (!(cin >> time.hours))
  85. {
  86. cin.clear();
  87. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  88. cout << "Invalid data.\n\n";
  89. continue;
  90. }
  91.  
  92. cout << "Minutes: ";
  93. if (!(cin >> time.minutes))
  94. {
  95. cin.clear();
  96. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  97. cout << "Invalid data.\n\n";
  98. continue;
  99. }
  100.  
  101. cout << "Seconds: ";
  102. if (!(cin >> time.seconds))
  103. {
  104. cin.clear();
  105. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  106. cout << "Invalid data.\n\n";
  107. continue;
  108. }
  109.  
  110. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  111.  
  112. validInput = isTimeValid(time);
  113.  
  114. if (!validInput)
  115. {
  116. cout << "Invalid data.\n\n";
  117. }
  118.  
  119. } while (validInput != true);
  120. } // end of getTime()
  121. // =============================================================
  122.  
  123.  
  124.  
  125. // ==== isTimeValid ============================================
  126. // Checks if the entered time is valid (24-hour format).
  127. //
  128. // Input:
  129. // time - (IN) Time struct containing hours, minutes, seconds
  130. //
  131. // Output:
  132. // Returns true if valid, false otherwise
  133. // =============================================================
  134. bool isTimeValid(const Time &time)
  135. {
  136. if (time.hours < 0 || time.hours > MAX_HOURS)
  137. return false;
  138.  
  139. if (time.minutes < 0 || time.minutes > MAX_MINS)
  140. return false;
  141.  
  142. if (time.seconds < 0 || time.seconds > MAX_SECS)
  143. return false;
  144.  
  145. return true;
  146. } // end of isTimeValid()
  147. // =============================================================
  148.  
  149.  
  150.  
  151. // ==== addOneSecond ===========================================
  152. // Adds one second to the given time and handles rollovers.
  153. //
  154. // Input:
  155. // time - (IN & OUT) Time struct passed by reference
  156. //
  157. // Output:
  158. // None
  159. // =============================================================
  160. void addOneSecond(Time &time)
  161. {
  162. time.seconds++;
  163.  
  164. if (time.seconds > MAX_SECS)
  165. {
  166. time.seconds = 0;
  167. time.minutes++;
  168.  
  169. if (time.minutes > MAX_MINS)
  170. {
  171. time.minutes = 0;
  172. time.hours++;
  173.  
  174. if (time.hours > MAX_HOURS)
  175. {
  176. time.hours = 0;
  177. }
  178. }
  179. }
  180. } // end of addOneSecond()
  181. // =============================================================
  182.  
  183.  
  184.  
  185. // ==== displayTime ============================================
  186. // Displays the time after adding one second in HH:MM:SS format.
  187. //
  188. // Input:
  189. // time - (IN) Time struct
  190. //
  191. // Output:
  192. // Prints formatted time
  193. // =============================================================
  194. void displayTime(const Time &time)
  195. {
  196. cout << "After adding one second, the time is ";
  197.  
  198. cout.fill('0');
  199. cout << setw(2) << time.hours << ":"
  200. << setw(2) << time.minutes << ":"
  201. << setw(2) << time.seconds << ".\n";
  202.  
  203. cout.fill(' ');
  204. } // end of displayTime()
  205. // =============================================================
  206.  
Success #stdin #stdout 0.01s 5292KB
stdin
14
44
22
n





stdout
Enter the time in "military time", (24-hour format),
in the following order: HH:MM:SS, (Hours, Minutes, Seconds).
Hours: Minutes: Seconds: After adding one second, the time is 14:44:23.
Do it again? (Y/N)