In: Computer Science
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input; and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.) c++
C++ Program:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//Function prototypes
void get_input(int &hour, int &minute); //- function sets
the hour and minute based on user input
void get_12_input(int &hour, int &minute, string
&am_pm); //- function sets the hour and minute based on user
input
void convert_to_12_hour(int &hour, int &minu, char
&am_pm); //- function converts the hour to 12-hour notation and
sets AM/PM
void convert_to_24_hour(int &hour, int &minu, string zone);
//- function converts the hour to 24-hour notation
void print_24_hour_time(int hour, int minute); //- function outputs
the 24-hour time.
void print_12_hour_time(int hour, int minute, char am_pm); //-
function outputs the 12-hour time.
void menu(); //Prints the menu
//Main function
int main()
{
int h, m;
int h24, m24;
char AM_PM;
string zone;
int ch;
//Menu
menu();
cin >> ch;
//Loop till user want to quit
while(ch != 3)
{
switch(ch)
{
case 1: //Reading input
get_12_input(h, m, zone);
//Conversion
convert_to_24_hour(h, m, zone);
h24 = h;
m24 = m;
//Printing
print_24_hour_time(h24, m24);
break;
case 2: //Reading input
get_input(h24, m24);
//Assigning values
h = h24;
m = m24;
//Conversion
convert_to_12_hour(h, m, AM_PM);
//Printing
print_12_hour_time(h, m, AM_PM);
break;
}
//Menu
menu();
cin >> ch;
}
return 0;
}
//Menu
void menu()
{
cout << "\n ***** MENU ***** \n 1 - 12Hr to 24Hr \n 2 - 24Hr
to 12Hr \n 3 - Quit \n Your selection: ";
}
//Function that reads a line of text from file
void get_input(int &h, int &m)
{
//Reading a line of input file
cout << "\nEnter the hour in 24-hour format: ";
cin >> h;
cout << "\nEnter the minute in 24-hour format: ";
cin >> m;
}
//Function that reads a line of text from file
void get_12_input(int &h, int &m, string &am_pm)
{
//Reading a line of input file
cout << "\nEnter the hour in 12-hour format: ";
cin >> h;
cout << "\nEnter the minute in 12-hour format: ";
cin >> m;
cout << "\nEnter Time Zone: ";
cin >> am_pm;
}
//Function that converts 24 hr format to 12 hr format
void convert_to_12_hour(int &h, int &m, char
&AM_PM)
{
//Converting time
if(h == 12)
{
AM_PM = 'P';
}
else if(h > 12)
{
h = h-12;
AM_PM = 'P';
}
else
{
AM_PM = 'A';
}
}
//Function that converts 12 hr format to 24 hr format
void convert_to_24_hour(int &h, int &m, string AM_PM)
{
//Converting time
if(AM_PM == "PM")
{
h = h+12;
}
}
//Function that prints the time format
void print_24_hour_time(int h, int m)
{
cout << "\nThe time in 24-hour format is ";
cout << setfill('0') << setw(2) << h << ":"
<< setfill('0') << setw(2) << m <<
"\n";
}
//Function that prints the time format
void print_12_hour_time(int h, int m, char AM_PM)
{
cout << "\nThe time in 12-hour format is ";
//If hours == 12
if(h == 12)
cout << setfill('0') << setw(2) << h << ":"
<< setfill('0') << setw(2) << m << " "
<< ((AM_PM=='A')?"AM":"PM") << " \n";
//Hours > 12
else if(h > 12)
cout << setfill('0') << setw(2) << (h-12)
<< ":" << setfill('0') << setw(2) << m
<< " " << ((AM_PM=='A')?"AM":"PM") << "
\n";
//Hours < 12
else
cout << setfill('0') << setw(2) << h << ":"
<< setfill('0') << setw(2) << m << " "
<< ((AM_PM=='A')?"AM":"PM") << " \n";
}
_________________________________________________________________________________________
Sample Run: