Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM.
(A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below:
void input(int& hours24, int& minutes);
//Precondition: input(hours, minutes) is called with
//arguments capable of being assigned values.
//Postcondition:
// user is prompted for time in 24 hour format:
// HH:MM, where 0 <= HH < 24, 0 <= MM < 60.
// hours is set to HH, minutes is set to MM. The output function will have the converted time displayed as per the declaration below: void output(int hours, int minutes, char AMPM); //Precondition:
// 0 < hours <=12, 0 <= minutes < 60,
// AMPM == 'P' or AMPM == 'A'
//Postcondition:
// time is written in the format
// HH:MM AM or HH:MM PM
A third function performs the actual conversion. 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 declaration or prototype of this third function is as follows:
void convert(int& hours, char& AMPM);
//Precondition: 0 <= hours < 24,
//Postcondition:
// if hours > 12,
// Note: definitely in the afternoon
// hours is replaced by hours - 12,
// AMPM is set to 'P'
// else if 12 == hours
// boundary afternoon hour
// AMPM is set to 'P',
// hours is not changed.
// else if 0 == hours
// boundary morning hour
// hours = hours + 12;
// AMPM = 'A';
// else
// (hours < 12)
// AMPM is set to 'A';
// hours is unchanged
(B) The following “int main()” uses the above three functions for time format conversion. It includes a “do-while” loop that lets the user repeat the time conversion computation for new input values repeatedly until the user says he or she wants to end the program by typing any character other than ‘Y’. Add code to make sure that the following input data conditions are met:
0 <= hours < 24, 0 <= minutes < 60
int main()
{
int hours, minutes;
char AMPM, ans;
do
{
input(hours, minutes);
// Add code to check 0 <= hours < 24, 0 <= minutes < 60
// Otherwise output an ERROR message and break
convert(hours, AMPM);
output(hours, minutes, AMPM);
cout << "Enter Y or y to continue," << " anything else quits." << endl;
cin >> ans;
} while ('Y'== ans || 'y' == ans);
return 0;
}
(C) Develop the definition of the three functions described in part (A).
(D) Run and verify your program. Copy and paste your C++ code from MS Visual Studio:
Copy and paste Console Debug Window results here:
#include <iostream>
#include <iomanip>
using namespace std;
void input(int& hours24, int& minutes);
void output(int hours, int minutes, char AMPM);
void convert(int& hours, char& AMPM);
int main()
{
int hours, minutes;
char AMPM, ans;
do
{
input(hours, minutes);
// validate hours is between [0, 23] and minutes is between
[0,59]
// if not display error message and exit the loop
if(hours < 0 || hours >= 24 && minutes < 0 ||
minutes >= 60)
{
cout<<"ERROR: Invalid input for time in 24-hour format.
Exiting the application."<<endl;
break;
}
convert(hours, AMPM);
output(hours, minutes, AMPM);
cout << "Enter Y or y to continue," << " anything else quits." << endl;
cin >> ans;
} while ('Y'== ans || 'y' == ans);
return 0;
}
//The user provides input as two integers separated by ":"
//Precondition: input(hours, minutes) is called with
//arguments capable of being assigned values.
//Postcondition:
// user is prompted for time in 24 hour format:
// HH:MM, where 0 <= HH < 24, 0 <= MM < 60.
// hours is set to HH, minutes is set to MM.
void input(int& hours24, int& minutes)
{
// input the hours , colon separator in sep and minutes
char sep;
cout<<"Enter the time in 24-hour format(HH:MM): ";
cin>>hours24>>sep>>minutes;
}
//The output function will have the converted time
displayed
//Precondition:
// 0 < hours <=12, 0 <= minutes < 60,
// AMPM == 'P' or AMPM == 'A'
//Postcondition:
// time is written in the format
// HH:MM AM or HH:MM PM
void output(int hours, int minutes, char AMPM)
{
// setfill is used to pad the integers with leading 0 so that hours
and minutes are displayed as 2 digit integers
// setw(2) is used to set the field width for hours and minutes to
2
cout<<setw(2)<<setfill('0')<<hours<<":"<<setw(2)<<setfill('0')<<minutes<<"
";
if(AMPM == 'P') // PM
cout<<"PM";
else // AM
cout<<"AM";
cout<<endl;
}
//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.
//Precondition: 0 <= hours < 24,
//Postcondition:
// if hours > 12,
// Note: definitely in the afternoon
// hours is replaced by hours - 12,
// AMPM is set to 'P'
// else if 12 == hours
// boundary afternoon hour
// AMPM is set to 'P',
// hours is not changed.
// else if 0 == hours
// boundary morning hour
// hours = hours + 12;
// AMPM = 'A';
// else
// (hours < 12)
// AMPM is set to 'A';
// hours is unchanged
void convert(int& hours, char& AMPM)
{
if(hours < 12) // AM
{
AMPM = 'A';
if(hours == 0) // hours = 0, set hours to 12 in 12-hour format,
else hours remains unchanged
{
hours = 12;
}
}
else // PM
{
AMPM = 'P';
if(hours != 12) // hours != 12, set hours to hours-12 to get the
hours in 12-hour format, else if hours = 12, then it remains
unchanged
hours = hours-12;
}
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.