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:
C++
#include <iostream>
#include<string>
using namespace std;
void choices()
{
cout<<("1.convert 24hr notation to 12hr notation\n2.1.convert
24hr notation to 12hr notation\n");
cout<<("Enter your choice:");
}
void convert_24hrto12hr(int hr,int min,int sec)
{
cout<<hr<<":"<<min<<":"<<sec<<"
in 12hr format is: ";
if(hr>=12)
{if(hr>12)
cout<<hr-12<<":"<<min<<":"<<sec<<"pm";
else
cout<<12<<":"<<min<<":"<<sec<<"pm";
}
else
{
cout<<hr<<":"<<min<<":"<<sec<<"am";
}
}
void convert_12hrto24hr(int hr,int min,int sec, string ampm
)
{
cout<<hr<<":"<<min<<":"<<sec<<"
"<<ampm<<" in 24hr format is: ";
if(ampm.compare("pm")==0)
{
cout<<hr+12<<":"<<min<<":"<<sec;
}
else
{
cout<<hr<<":"<<min<<":"<<sec;
}
}
int main()
{
choices();
int i;
cin>>i;
if(i==1)
{
int hr,min,sec;
cout<<"Enter hours:";cin>>hr;
cout<<"Enter minutes:";cin>>min;
cout<<"Enter seconds:";cin>>sec;
convert_24hrto12hr(hr,min,sec);
}
else if(i==2)
{
int hr,min,sec;
string ampm;
cout<<"Enter hours:";cin>>hr;
cout<<"Enter minutes:";cin>>min;
cout<<"Enter seconds:";cin>>sec;
cout<<"Enter am / pm:";cin>>ampm;
convert_12hrto24hr(hr,min,sec,ampm );
}
else
{
cout<<"Invalid choice";
}
return 0;
}