Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. main()
  9. {
  10. char repeat = 'Y';
  11. cout << "This program will convert 12-hour time format into 24-hour time format." << endl;
  12. cout << "Please make sure the time you input is in the correct format as stated below, WITH THE SPACE between the time and AM/PM." << endl;
  13.  
  14. do {
  15.  
  16. int hour; //Collects the hour.
  17. char colon; //Collects the colon from the time.
  18. int minutes; //Collects the minute.
  19. string ampm; //Collects the AM/PM ending.
  20.  
  21. cout << "Pleas enter a time in 12-hour format (HH:MM AM/PM)." << endl;
  22. cin >> hour >> colon >> minutes >> ampm;
  23.  
  24. if (hour > 12 || hour <= 0 || minutes < 0 || minutes > 59) //Checks to make sure the time is valid.
  25. {
  26. cout << "Please enter the time in the correct format." << endl;
  27. hour = 0;
  28. continue;
  29. }
  30.  
  31. //Following block converts the time from 12-hour to 24-hour by adding 12 to hour if the time is PM. If AM and hour is 12, hour is turned into 00.
  32.  
  33. int i = 0; //Int for the loop and the loop only.
  34.  
  35. do { //Loop that can't be broken unless broken out of.
  36. if (ampm == "AM" && hour == 12)
  37. {
  38. hour = 0; //Sets hour to 0 since it's the morning.
  39. if (minutes < 10)
  40. {
  41. cout << "0" << hour << "0" << minutes << endl;
  42. break;
  43. }
  44. cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front
  45. break; //Breaks out of the loop
  46. }
  47. else if (ampm == "PM")
  48. {
  49. hour += 12; //Adds 12 to the hour to convert it to 24-hour format.
  50. if (hour < 10)
  51. {
  52. if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
  53. {
  54. cout << "0" << hour << "0" << minutes << endl;
  55. break;
  56. }
  57. cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front to fit format.
  58. break; //Breaks out of the loop
  59. }
  60. if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
  61. {
  62. cout << hour << "0" << minutes << endl;
  63. break;
  64. }
  65. cout << hour << minutes << endl; //Outputs the converted time.
  66. break; //Breaks out of the loop
  67. }
  68. else
  69. {
  70. hour = hour;
  71. if (hour < 10)
  72. {
  73. if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
  74. {
  75. cout << "0" << hour << "0" << minutes << endl;
  76. break;
  77. }
  78. cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front to fit format.
  79. break; //Breaks out of the loop
  80. }
  81. if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
  82. {
  83. cout << hour << "0" << minutes << endl;
  84. break;
  85. }
  86. cout << hour << minutes << endl; //Outputs the converted time.
  87. break; //Breaks out of the loop
  88. }
  89. } while(i == 0);
  90.  
  91. //Function repeat block
  92. cout << "Would you like to convert another time? (Y/N)" << endl;
  93. cin >> repeat;
  94. toupper(repeat);
  95.  
  96. } while (repeat == 'Y');
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement