Reference Parameters (returning multiple values): Write a C++ function that converts standard time to military time. Inputs include hours and minutes in standard time and a character equal to ‘a’ for am or ‘p’ for pm.

The function call might look like:

MilitaryTime(SHour, SMin, AorP, MHour, MMin);

Also write a main program to prompt the user for the inputs (such as 1:30 am), call the function, and display the input and the output in the following form:

12:30 am= 0030
2:30 am = 0230
3:30 pm = 1530
12:00 am = 0000

Run the program for the four cases above plus at least three other cases.

Hint: Using fill(’0’) is an easy way to show leading zeros.

Respuesta :

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;

Ver imagen hamzafarooqi188