Advertisement
wafflecone67

militime3

Apr 22nd, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. //Output
  2.  
  3. A military time of 0012:3 = 12:12:3
  4. A military time of 1145:5 = 11:45:5
  5. A military time of 1419:34 = 2:19:34
  6.  
  7. Enter a number of military hours in the format #### followed by a space and then
  8. a number of seconds: 2401
  9. 62
  10. Sorry, that wasn't a valid time. Try again.
  11. Enter a number of military hours in the format #### followed by a space and then
  12. a number of seconds:
  13.  
  14.  
  15. //Source code
  16. //Christopher Collins
  17. //Comp sci 2
  18. //4/25/19
  19. //MilTime.cpp
  20. //MilTime.cpp holds the implementation of methods from the MilTime.h file
  21. //MilTime.cpp holds the implementation of methods from the MilTime.h file
  22. #include "MilTime.h"
  23. using namespace std;
  24.  
  25. void MilTime::setTimeVars()
  26. {
  27. //set Time min and sec directly
  28. sec = milSeconds;
  29. min = stoi(milHours.substr(2, 2)); //pull off the last two digits of the military hour
  30. int tempH = stoi(milHours.substr(0, 2)); //pull off the first two digits
  31. if (tempH == 0) //handle case of midnight hour
  32. hour = 12;
  33. else if (tempH >= 1 && tempH <= 12)
  34. hour = tempH; //no conversion needed
  35. else
  36. //subtract 12 from larger num to get regular hour
  37. hour = tempH - 12;
  38. }
  39.  
  40. bool MilTime::setTime(string milh, int mils)
  41. {
  42. bool retVal = false;
  43. if (stoi(milh) >= 0 && stoi(milh) <= 2359 && mils >= 0 && mils <= 59)
  44. {
  45. milHours = milh;
  46. milSeconds = mils;
  47. setTimeVars();
  48. retVal = true;
  49. }
  50. return retVal;
  51. }
  52. //Time.h
  53. #pragma once
  54. //Time.h file from page 972 in the 9/e of the book
  55. class Time
  56. {
  57. protected:
  58. int hour;
  59. int min;
  60. int sec;
  61. public:
  62. Time()
  63. {
  64. hour = 0; min = 0; sec = 0;
  65. }
  66. Time(int h, int m, int s)
  67. {
  68. hour = h; min = m; sec = s;
  69. }
  70. int getHour() const
  71. {
  72. return hour;
  73. }
  74. int getMin() const
  75. {
  76. return min;
  77. }
  78. int getSec() const
  79. {
  80. return sec;
  81. }
  82. };
  83.  
  84. #pragma once
  85.  
  86.  
  87. //MilTime.h
  88. //holds military time in hours and seconds (i.e. 1430 with 3 seconds)
  89. //inherits from Time class provided
  90. #include "Time.h"
  91. #include <cstdlib>
  92. #include <string>
  93. #include<iostream>
  94. using namespace std;
  95.  
  96. class MilTime : public Time
  97. {
  98. private:
  99. string milHours;
  100. int milSeconds;
  101. void setTimeVars();
  102. public:
  103. MilTime() : MilTime("0000", 0)
  104. {}
  105. MilTime(string milh, int mils)
  106. {
  107. setTime(milh, mils);
  108. }
  109. bool setTime(string, int);
  110. string getHour() const
  111. {
  112. return milHours;
  113. }
  114. int getStandHr() const
  115. {
  116. return hour;
  117. }
  118.  
  119. };
  120. class BadHours :public exception {
  121. public:BadHours() {
  122. }
  123. const char* x() const throw()
  124. {
  125. return "Invalid number for military time hours\n";
  126.  
  127. }
  128. };
  129. class BadSeconds :public exception {
  130. public:BadSeconds() {
  131. }
  132. const char* x() const throw()
  133. {
  134. return "Invalid number for military time seconds\n";
  135.  
  136. }
  137. };
  138.  
  139.  
  140.  
  141. //Main.cpp
  142. #include "Miltime.h"
  143. #include "Time.h"
  144. #include <iostream>
  145. #include <string>
  146. #include "main.h"
  147. using namespace std;
  148.  
  149. int main()
  150. {
  151. MilTime m1("0012", 3);
  152. int mSeconds;
  153. string mHours;
  154. int converter;
  155.  
  156.  
  157. cout << "A military time of ";
  158.  
  159. cout << m1.getHour() << ":";
  160.  
  161. cout << m1.getSec() << " = ";
  162.  
  163. cout << m1.getStandHr();
  164.  
  165. cout << ":" << m1.getMin() << ":";
  166.  
  167. cout << m1.getSec() << endl;
  168.  
  169. m1.setTime("1145", 5);
  170.  
  171.  
  172. cout << "A military time of ";
  173.  
  174. cout << m1.getHour() << ":";
  175.  
  176. cout << m1.getSec() << " = ";
  177.  
  178. cout << m1.getStandHr() << ":";
  179.  
  180. cout << m1.getMin() << ":";
  181.  
  182. cout << m1.getSec() << endl;
  183.  
  184. m1.setTime("1419", 34);
  185.  
  186. cout << "A military time of " << m1.getHour();
  187.  
  188. cout << ":" << m1.getSec();
  189.  
  190. cout << " = " << m1.getStandHr() << ":";
  191.  
  192. cout << m1.getMin() << ":";
  193.  
  194. cout << m1.getSec() << endl;
  195.  
  196. cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
  197. cin >> mHours;
  198. converter = stoi(mHours);
  199. cin >> mSeconds;
  200. try {
  201.  
  202. if (converter > 0000 && converter <= 2359)//This is the input validation for the string it converts mhours from a string to an int
  203. {//And it does this inorder for it to make the input validation for greater than less than easier
  204.  
  205. if (mSeconds > 00 && mSeconds < 58)
  206. {
  207.  
  208.  
  209. cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
  210. cin >> mHours >> mSeconds;
  211. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  212. }
  213. else
  214. {
  215. BadSeconds sec;
  216. throw sec;
  217. }
  218. }
  219. else
  220. {
  221. BadHours hr;
  222. throw hr;
  223. }
  224. }
  225. catch (BadHours hr)
  226. {
  227. cout << hr.x();
  228.  
  229. }
  230. catch (BadSeconds sec)
  231. {
  232. cout << sec.x();
  233.  
  234. }
  235. system("pause");
  236. }
  237.  
  238. {
  239. throw "Improper output exception 36";
  240. }
  241. }
  242. try
  243. {
  244. cin >> mSeconds;
  245. }
  246. catch (const std::exception&)
  247. {
  248. if (mSeconds >= 61 || mSeconds <= -1)
  249. {
  250. throw "Improper output exception 36";
  251. }
  252.  
  253. }
  254. isValid = m1.setTime(mHours, mSeconds);
  255.  
  256.  
  257. while (!isValid)
  258. {
  259. cout << "Sorry, that wasn't a valid time. Try again.";
  260. cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
  261. cin >> mHours >> mSeconds;
  262. isValid = m1.setTime(mHours, mSeconds);
  263. }
  264. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  265.  
  266. system("pause");
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement