Professional Documents
Culture Documents
notation. For example, it should convert 14:25 to 2:25PM. The input is given as
two integers. There should be at least three functions, one for input, one to do
the conversion, and one for output. Record the AM/PM information as a value of
type char 'A' for AM and 'P' for PM. Thus, the function for doing the conversion
s will have a call-by-reference formal parameter of type char to record whether
it is AM or PM. (The function will have other parameters as well). Include a loo
p that lets user repeat this computation for new input values again and again un
til the user says he or she wants to end the program.
/*** ch4p1.cpp ***/
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
/***
void
char
void
int main(void)
{
int hour, minute;
char ampm;
char ans;
do
{
clrscr();
cout << "This program converts from twenty-four hour\n"
<< "notation to twelve-hour notation. Example,\n"
<< "14:25 will be converted to 2:25 PM.\n\n";
input(hour, minute);
ampm = convert24to12(hour);
output(hour, minute, ampm);
cout << "\n\nContinue (y/n) ";
cin >> ans;
} while (tolower(ans) == 'y');
return 0;
}
/////////////////////////////////////////////////////////////////////
/////////////////////////
// get input from user //
/////////////////////////
void input(int& h, int& m)
{
/*** get hour in 24 hour notation ***/
do
{
cout << "\nEnter hour (0 - 23): ";
cin >> h;
if (h >= 12)
if (h == 12)
return 'P';
else
{
h -= 12;
return 'P';
}
return 'A';
} // end convert24to12()
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
// display hour in 12 hour notation with AM or PM //
////////////////////////////////////////////////////
void output(int& h, int& m, char ampm)
{
cout << "\n\t" << h << ':';
if (m < 10)
cout << m << ' ' << ampm << 'M' << endl;
} // end output()
/////////////////////////////////////////////////////////////////////