Guest User

Untitled

a guest
Dec 9th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void convert(int& hours, char& amPm);
  7. void input(int& hours, int& minutes);
  8. void output(int hours, int minutes, char amPm);
  9. int main()
  10. {
  11.     int hours, minutes;
  12.     char convAgain;
  13.     char amPm;
  14.  
  15.     do
  16.     {
  17.         input(hours, minutes);
  18.         convert(hours, amPm);
  19.         output(hours, minutes, amPm);
  20.         cout << endl << "Again? ";
  21.         cin >> convAgain;
  22.     }
  23.     while(convAgain == 'y' || convAgain == 'Y');
  24.  
  25.     return 0;
  26. }
  27.  
  28. void input(int& hours, int& minutes)
  29. {
  30.     cout << "Enter hour: ";
  31.     cin >> hours;
  32.  
  33.     while (hours > 23 || hours < 0)
  34.     {
  35.         cout << "Please enter a hour between 0 and 23: " << endl;
  36.         cin >> hours;
  37.     }
  38.  
  39.     cout << "Enter minutes: ";
  40.     cin >> minutes;
  41.  
  42.     while (minutes > 59  || minutes <0)
  43.     {
  44.         cout << "Please enter minutes between 0 and 59: " << endl;
  45.         cin >> minutes;
  46.     }
  47. }
  48.  
  49. void convert(int& hours, char& amPm)
  50. {
  51.     if (hours > 12)
  52.     {
  53.         amPm = 'P';
  54.         hours -= 12;
  55.     }
  56.     if (hours == 0)
  57.     {
  58.         hours = 12;
  59.         amPm = 'A';
  60.     }
  61.  
  62.     //if (hours >= 12)
  63.         //mPm = 'P';
  64.     //else
  65.         //amPm = 'A';
  66.  
  67. }
  68. void output(int hours, int minutes, char amPm)
  69. {
  70.     string amPmHours;
  71.     switch(amPm)
  72.     {
  73.         case 'p':
  74.         case 'P':
  75.             amPmHours = "PM";
  76.             break;
  77.         case 'a':
  78.         case 'A':
  79.             amPmHours = "AM";
  80.             break;
  81.     }
  82.  
  83.     if(amPm == 'a' | amPm == 'A')
  84.     {
  85.         if (hours < 12)
  86.         {
  87.             if (minutes <= 9)
  88.                 cout << hours << ":" << setw(2) << setfill('0') <<  minutes << amPmHours;
  89.  
  90.             else
  91.                 cout << hours << ":" << minutes << amPmHours;
  92.         }
  93.  
  94.         if (hours > 12)
  95.         {
  96.             if(minutes <= 9)
  97.                 cout << setw(2) << setfill('0') << hours << ":" << minutes << amPmHours;
  98.             else
  99.                 cout << hours << ":" << minutes << amPmHours;
  100.         }
  101.  
  102.     if (amPm == 'p' | amPm == 'P')
  103.     {
  104.         if (hours >= 12)
  105.         {
  106.             if (minutes <= 9)
  107.                 cout << hours << ":" << setw(2) << setfill('0') <<  minutes << amPmHours;
  108.  
  109.             else
  110.                 cout << hours << ":" << minutes << amPmHours;
  111.         }
  112.  
  113.         if (hours < 12)
  114.         {
  115.             if(minutes <= 9)
  116.                 cout << setw(2) << setfill('0') << hours << ":" << minutes << amPmHours;
  117.             else
  118.                 cout << hours << ":" << minutes << amPmHours;
  119.         }
  120.  
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment