Skip to content

Instantly share code, notes, and snippets.

@crushedhat
Last active March 18, 2018 20:22
Show Gist options
  • Save crushedhat/e808a7e9672040d80ac06751b1b0e768 to your computer and use it in GitHub Desktop.
Save crushedhat/e808a7e9672040d80ac06751b1b0e768 to your computer and use it in GitHub Desktop.
/* Joseph Mora
CS65
18Mar18*/
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
//void has no expectation of a return value
void standardConvert(int hour, int min, int AM_PM){
//setting width of integers and padding with 0's if it needs to be filled
if ((AM_PM == 2) && (hour == 12)){
cout << "Military Time: " << hour << setfill('0') << setw(2) << min << endl;
}
else if((AM_PM == 1) && (hour == 12)){
if (min == 0){
hour = 0;
cout << "Military Time: " << setfill('0') << setw(4) << hour << endl;
}
else{
hour = 0;
cout << "Military Time: " << setfill('0') << setw(2) << hour << setfill('0') << setw(2) << min << endl;
}
}
else if((AM_PM == 1) && (hour < 10)){
cout << "Military Time: " << setfill('0') << setw(2) << hour << setfill('0') << setw(2) << min << endl;
}
else if(((AM_PM == 1) && (hour >= 10) && (hour < 12))){
cout << "Military Time: " << hour << min << endl;
}
else {
hour += 12;
cout << "Military Time: " << hour << min << endl;
}
}
void militaryConvert(int hour, int min){
if(hour == 0){
hour +=12;
cout << "Standard Time: " << hour << ":" << setfill('0') << setw(2) << min << " AM" << endl;
}
else if(hour == 12){
cout << "Standard Time: " << hour << ":" << setfill('0') << setw(2) << min << " PM" << endl;
}
else if(hour > 12){
hour -= 12;
cout << "Standard Time: " << hour << ":" << setfill('0') << setw(2) << min << " PM" << endl;
}
else {
cout << "Standard Time: " << hour << ":" << setfill('0') << setw(2) << min << " AM" << endl;
}
}
int main(){
int min;
int hour;
int AM_PM;
int input;
bool done = false;
while(!done){
cout << "====================Main Menu===================" << endl;
cout << "1. Convert from Standard to Military time format." << endl;
cout << "2. Convert from Military to Standard time format." << endl;
cout << "3. Exit program." << endl;
cin >> input;
switch (input){
case 1:
cout << "time(hour): ";
cin >> hour;
cout << "time(min): ";
cin >> min;
if(hour <= 12){
cout << "(1) Type 1 for AM" << endl;
cout << "(2) Type 2 for PM" << endl;
cin >> AM_PM;
if (AM_PM == 1){
cout << "You entered " << hour << ":" << setfill('0') << setw(2) << min << " AM" << endl;
}
else{
cout << "You entered " << hour << ":" << setfill('0') << setw(2) << min << " PM" << endl;
}
standardConvert(hour, min, AM_PM);
}
break;
case 2:
cout << "time(hour): ";
cin >> hour;
cout << "time(min): ";
cin >> min;
militaryConvert(hour, min);
break;
case 3:
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment