SHARE
HELP

To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

# Absolute C++

Authors:
Walter Savitch ,kenrick Mock
Exercise:
Programming Projects
Chapter:
Edition:
5
ISBN:
9780132846813
Question:
1
 Previous Next

### Question

Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. 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 A.M./P.M. information as a value of type char , 'A' for A.M. and 'P' for P.M. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is A.M. or P.M. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.

Program:

``````#include<iostream>

void overviewData( );
void inputData(int &hr,int &min);
void convert12Format(int &hr,char &type);
void displayData(int hr,int min,char type);

int main()
{
using namespace std;
int hours,min;
char type,choice;
overviewData( );
do
{
inputData(hours,min);
convert12Format(hours,type);
displayData(hours,min,type);
cout<<"To continue then press'y':";
cin>>choice;
}while(choice=='y');
}
void overviewData( )
{
using namespace std;
cout << "Converts 24-hour to 12-notation.\n";
cout<<"-----------------------------------\n";
}
//Read hours and minutes from the key board
void inputData(int &hr,int &min)
{

using namespace std;
cout<<"Enter hours:";
cin>>hr;
cout<<"Enter minutes:";
cin>>min;
}
//Convert 24 hours format to 12 hours format
void convert12Format(int &hrs,char &type)
{
if(hrs>12 )
{
hrs=hrs%12;
type='P';
}
else
type='A';
}
//Result on screen
void displayData(int hrs,int min,char t)
{
using namespace std;
cout<<"12-hour notation:"<<endl;
cout<<hrs<<":"<<min<<" "<<t<<"M"<<endl;
}``````

Output:

``````Converts 24-hour to 12-notation.
-------------------------------------------
Enter hours:14 25
Enter minutes:12-hour notation:
2:25 PM
To continue then press'y':y
Enter hours:23 00
Enter minutes:12-hour notation:
11:0 PM
To continue then press'y':n``````

### Discussions

Post the discussion to improve the above solution.