Can you help me this C++ programing? Draw a flow chart before write a program. W
ID: 3834318 • Letter: C
Question
Can you help me this C++ programing?
Draw a flow chart before write a program.
Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. 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 conversions 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 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.
Explanation / Answer
#include<iostream>
using namespace std;
void input(int& hours, int& minutes, char& ampm)
{
do
{
cout << "Enter hours between 0 to 23: ";
cin >> hours;
if(hours > 23 || hours<0) cout << "you have entered incorrecet values.Please try again" << endl;
}while(hours > 23 || hours<0);
do
{
cout << "Enter minutes between 0 to 59: ";
cin >> minutes;
if(minutes > 59 || minutes<0) cout << "you have entered incorrecet values.Please try again" << endl;
}
while(minutes > 59 || minutes<0);
do
{
cout << "Enter A for AM and P for PM: ";
cin >> ampm;
if(ampm !='A' && ampm!='P') cout << "you have entered incorrecet values.Please try again" << endl;
}
while(ampm !='A' && ampm!='P');
}
void convert(int& hours, int& minutes, char& ampm)
{
if (hours > 12)
{
//Afternoon
hours = hours - 12;
ampm = 'P';
}
else if (12 == hours)
{
//Also afternoon
ampm = 'P'; //But the hours don't change
}
else if (0 == hours)
{
//Morning
hours = hours + 12;
ampm = 'A';
}
else
{
//Hours < 12, so morning hour
ampm = 'A'; //Hours don't change
}
}
void output(int hours, int mins, char ampm)
{
string col = ":";
cout << "The time in 12-hour notation is: " << endl;
if (mins < 10) {
col = ":0"; //So we'll get a zero before the minutes
}
cout << hours << col << mins << " " << ampm << "M" << endl;
}
int main(int argc,char **argv)
{
int hours, minutes;
char ampm;
char choice;
do
{
input(hours, minutes, ampm);
convert(hours, minutes, ampm);
output(hours, minutes, ampm);
cout << endl << "Enter Y to run again, any other key to exit: ";
cin >> choice;
}
while(choice == 'y' || choice == 'Y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.