Reference Parameters (returning multiple values): Write a C++ function that conv
ID: 3765199 • Letter: R
Question
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.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void MilitaryTime(int SHour, int SMin, char c[2])
{
int MHour;
if(c[0]=='P'|| c[0]=='p')
{
MHour= 12+SHour;
if(MHour==24)
MHour=0;
}
else
{
MHour=SHour;
}
cout<<SHour<<":"<<SMin<<"
"<<c<<"="<<MHour<<SMin;
}
void main()
{
int SHour,SMin;
char c[2];
cout<<"Enter Standard Hours";
cin>>SHour;
cout<<"Enter Standard Minutes";
cin>>SMin;
cout<<"Enter Am/Pm";
cin>>c;
MilitaryTime(SHour, SMin, c);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.