Answer:
Code is given as below:
Explanation:
#include <iostream>
using namespace std;
//function prototype declaration
void MilitaryTime(int, int, char, int &, int &);
int main()
{
//declare required variables
int SHour, SMin, MHour, MMin;
char AorP;
//promt and read the hours from the user
cout<<"Enter hours in standard time : ";
cin>>SHour;
//check the hours are valid are not
while(SHour<0 || SHour>12)
{
cout<<"Invalid hours for standard time. "
<<"Try again..."<<endl;
cout<<"Enter hours in standard time : ";
cin>>SHour;
}
//promt and read the minutes from the user
cout<<"Enter minutes in standard time : ";
cin>>SMin;
//check the minutes are valid are not
while(SMin<0 || SMin>59)
{
cout<<"Invalid minutes for standard time. "
<<"Try again..."<<endl;
cout<<"Enter minutes in standard time : ";
cin>>SMin;
}
//promt and read the am or pm from the user
cout<<"Enter standard time meridiem (a for AM p for PM): ";
cin>>AorP;
//check the meridiem is valid are not
while(!(AorP=='a' || AorP=='p' || AorP=='A' || AorP=='P'))
{
cout<<"Invalid meridiem for standard time. "
<<"Try again..."<<endl;
cout<<"Enter standard time meridiem (a for AM p for PM): ";
cin>>AorP;
}
//call function to calculate the military time
MilitaryTime(SHour, SMin, AorP, MHour, MMin);
//fill zeros and display standard time
cout.width(2);
cout.fill('0');
cout<<SHour<<":";
cout.width(2);
cout.fill('0');
cout<<SMin;
if(AorP=='a' || AorP=='A')
cout<<" am = ";
else
cout<<" pm = ";
//fill zeros and display military time
cout.width(2);
cout.fill('0');
cout<<MHour;
cout.width(2);
cout.fill('0');
cout<<MMin<<endl;
system("PAUSE");
return 0;
}
//function to calculate the military time with reference parameters
void MilitaryTime(int SHour, int SMin, char AorP, int &MHour, int &MMin)
{
//check the meredium is am or pm
//and calculate hours
if(AorP=='a' || AorP=='A')
{
if(SHour==12)
MHour = 0;
else
MHour = SHour;
}
else
MHour = SHour+12;
MMin = SMin;