Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <vector>
- using namespace std;
- main()
- {
- char repeat = 'Y';
- cout << "This program will convert 12-hour time format into 24-hour time format." << endl;
- 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;
- do {
- int hour; //Collects the hour.
- char colon; //Collects the colon from the time.
- int minutes; //Collects the minute.
- string ampm; //Collects the AM/PM ending.
- cout << "Pleas enter a time in 12-hour format (HH:MM AM/PM)." << endl;
- cin >> hour >> colon >> minutes >> ampm;
- if (hour > 12 || hour <= 0 || minutes < 0 || minutes > 59) //Checks to make sure the time is valid.
- {
- cout << "Please enter the time in the correct format." << endl;
- hour = 0;
- continue;
- }
- //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.
- int i = 0; //Int for the loop and the loop only.
- do { //Loop that can't be broken unless broken out of.
- if (ampm == "AM" && hour == 12)
- {
- hour = 0; //Sets hour to 0 since it's the morning.
- if (minutes < 10)
- {
- cout << "0" << hour << "0" << minutes << endl;
- break;
- }
- cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front
- break; //Breaks out of the loop
- }
- else if (ampm == "PM")
- {
- hour += 12; //Adds 12 to the hour to convert it to 24-hour format.
- if (hour < 10)
- {
- if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
- {
- cout << "0" << hour << "0" << minutes << endl;
- break;
- }
- cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front to fit format.
- break; //Breaks out of the loop
- }
- if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
- {
- cout << hour << "0" << minutes << endl;
- break;
- }
- cout << hour << minutes << endl; //Outputs the converted time.
- break; //Breaks out of the loop
- }
- else
- {
- hour = hour;
- if (hour < 10)
- {
- if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
- {
- cout << "0" << hour << "0" << minutes << endl;
- break;
- }
- cout << "0" << hour << minutes << endl; //Outputs the time with 0 at front to fit format.
- break; //Breaks out of the loop
- }
- if (minutes < 10) //Checks to make sure the minutes will have a 0 in front if less than 10.
- {
- cout << hour << "0" << minutes << endl;
- break;
- }
- cout << hour << minutes << endl; //Outputs the converted time.
- break; //Breaks out of the loop
- }
- } while(i == 0);
- //Function repeat block
- cout << "Would you like to convert another time? (Y/N)" << endl;
- cin >> repeat;
- toupper(repeat);
- } while (repeat == 'Y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement