You are on page 1of 3

Write a program that converts from twenty-four-hour notation to twelve-hour

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

// standard input and output


// clrscr()
// tolower()

Prototype Declarations ***/


input(int&, int&);
// gets input at prompt
convert24to12(int&);
// computes hour and returns 'A' or 'P'
output(int&, int&, char); // diplay output

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 < 0 || h > 23)


cout << "\n\nInvalid Entry...";
} while (h < 0 || h > 23);
/*** get minute of the hour (0 to 59) ***/
do
{
cout << "\nEnter minute (0 - 59): ";
cin >> m;
if (m < 0 || m > 59)
cout << "\n\nInvalid Entry...";
} while (m < 0 || m > 59);
} // end input()
/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////
// converts 24 hour to 12 hour notation. //
// also retuns 'A' or 'P' for output.
//
///////////////////////////////////////////
char convert24to12(int& h)
{
if (h == 0)
{
h = 12;
return 'A';
}

// if midnight, (0 in 24 hour notation)

if (h >= 12)
if (h == 12)
return 'P';
else
{
h -= 12;
return 'P';
}

// if hour is greater or equal to 12, it's PM


// if hour is 12 noon, don't subtract 12 hours

return 'A';

// return 'A' if hour < 12

// assign 12 to hour for display

// if hour is > 12, subtract 12 hours

} // end convert24to12()
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
// display hour in 12 hour notation with AM or PM //
////////////////////////////////////////////////////
void output(int& h, int& m, char ampm)
{
cout << "\n\t" << h << ':';

// print the hour and ':'

if (m < 10)

// print a '0' before

cout << '0';

// 1 to 9 minute. example: 4:03 PM

cout << m << ' ' << ampm << 'M' << endl;
} // end output()
/////////////////////////////////////////////////////////////////////

You might also like