In: Computer Science
Project 1 - 24 hour to 12 hour conversion
write in c++
Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is AM or PM. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.
Each iteration of the loop will process 3 inputs:
You mustimplement 4 functions. You are notpermitted to change the name or signature of these functions; the testing framework is going to directly call the functions. The three functions and their signatures are (use the signatures as a hint for how to implement this!):
Sample execution:
Enter the hour in 24-hour format: 9 Enter the minute in 24-hour format: 13 The time in 24-hour format is 09:13 The time in 12-hour format is 09:13AM Continue? (y/n): y Enter the hour in 24-hour format: 12 Enter the minute in 24-hour format: 55 The time in 24-hour format is 12:55 The time in 12-hour format is 12:55PM Continue? (y/n): y Enter the hour in 24-hour format: 14 Enter the minute in 24-hour format: 05 The time in 24-hour format is 14:05 The time in 12-hour format is 02:05PM Continue? (y/n): n
input code:


output:

code:
#include <iostream>
#include<iomanip>
using namespace std;
/*get input function*/
void get_input(int &hour, int &minute)
{
/*take input from user and store into variables*/
cout<<"Enter the hour in 24-hour format:";
cin>>hour;
/*take minute input*/
cout<<"Enter the minute in 24-hour format:";
cin>>minute;
}
/*convert to 12 forma*/
void convert_to_12_hour(int &hour,char &am_pm)
{
/*if greater then 12*/
if(hour>11)
{
/*convert to 12*/
if(hour>12)
{
hour=hour%12;
}
else
{
/*for set 12*/
hour=hour;
}
/*set am pm*/
am_pm='P';
}
else
{
hour=hour;
/*set am pm*/
am_pm='A';
}
}
/*print in 24 format*/
void print_24_hour_time(int hour,int minute)
{
/*print*/
cout<<"The time in 24-hour format is
"<<setw(2)<<setfill('0')
<<hour<<":"<<setw(2)<<setfill('0')<<minute<<endl;
}
/*print in 12 hour format*/
void print_12_hour_time(int hour,int minute,char am_pm)
{
/*if A than print this*/
if(am_pm=='A')
{
cout<<"The time in 24-hour format is
"<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<"AM"<<endl;
}
else
{
/*else this*/
cout<<"The time in 24-hour format is
"<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<"PM"<<endl;
}
}
int main()
{
/*declare the variables*/
int hour,min;
char am_pm,ch=' ';
do
{
cout<<endl;
/*call the function*/
get_input(hour,min);
print_24_hour_time(hour,min);
convert_to_12_hour(hour,am_pm);
print_12_hour_time(hour,min,am_pm);
/*ask repeat or not*/
cout<<"Continue>(y/n):";cin>>ch;
}while(ch!='n');
return 0;
}