Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- void convert(int& hours, char& amPm);
- void input(int& hours, int& minutes);
- void output(int hours, int minutes, char amPm);
- int main()
- {
- int hours, minutes;
- char convAgain;
- char amPm;
- do
- {
- input(hours, minutes);
- convert(hours, amPm);
- output(hours, minutes, amPm);
- cout << endl << "Again? ";
- cin >> convAgain;
- }
- while(convAgain == 'y' || convAgain == 'Y');
- return 0;
- }
- void input(int& hours, int& minutes)
- {
- cout << "Enter hour: ";
- cin >> hours;
- while (hours > 23 || hours < 0)
- {
- cout << "Please enter a hour between 0 and 23: " << endl;
- cin >> hours;
- }
- cout << "Enter minutes: ";
- cin >> minutes;
- while (minutes > 59 || minutes <0)
- {
- cout << "Please enter minutes between 0 and 59: " << endl;
- cin >> minutes;
- }
- }
- void convert(int& hours, char& amPm)
- {
- if (hours > 12)
- {
- amPm = 'P';
- hours -= 12;
- }
- if (hours == 0)
- {
- hours = 12;
- amPm = 'A';
- }
- //if (hours >= 12)
- //mPm = 'P';
- //else
- //amPm = 'A';
- }
- void output(int hours, int minutes, char amPm)
- {
- string amPmHours;
- switch(amPm)
- {
- case 'p':
- case 'P':
- amPmHours = "PM";
- break;
- case 'a':
- case 'A':
- amPmHours = "AM";
- break;
- }
- if(amPm == 'a' | amPm == 'A')
- {
- if (hours < 12)
- {
- if (minutes <= 9)
- cout << hours << ":" << setw(2) << setfill('0') << minutes << amPmHours;
- else
- cout << hours << ":" << minutes << amPmHours;
- }
- if (hours > 12)
- {
- if(minutes <= 9)
- cout << setw(2) << setfill('0') << hours << ":" << minutes << amPmHours;
- else
- cout << hours << ":" << minutes << amPmHours;
- }
- if (amPm == 'p' | amPm == 'P')
- {
- if (hours >= 12)
- {
- if (minutes <= 9)
- cout << hours << ":" << setw(2) << setfill('0') << minutes << amPmHours;
- else
- cout << hours << ":" << minutes << amPmHours;
- }
- if (hours < 12)
- {
- if(minutes <= 9)
- cout << setw(2) << setfill('0') << hours << ":" << minutes << amPmHours;
- else
- cout << hours << ":" << minutes << amPmHours;
- }
- }
- }
Add Comment
Please, Sign In to add comment