C++ Include comments Write a program to convert the time from 24-hour notation t
ID: 3758083 • Letter: C
Question
C++
Include comments
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void input24(int hours, int minutes);
void output12(int hours, int minutes, char type);
void convert24To12Hour(int hours, char type);
int main()
{
int hours;
int minutes;
char type;
char pm;
char A, P;
cout << "Enter the number hours";
cin >> hours;
cout << "Enter the number minute";
cin >> minutes;
input(hours, minutes);
hours = convert24To12Hour(hours, type);
output(hours, minutes, type);
system("pause")
return 0;
}
void input24(int hours, int minutes)
{
cout << "Enter the hours for the 24 hour time: ";
cin >> hours;
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
}
void output12(int hours, int minutes, char type)
{
cout << "The time converted to 12 hour format is: " << hours << ":";
cout << minutes;
if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
}
void convert24To12Hour(int hours, char type)
{
hours = hours % 12;
}
if( hours < 12)
{
pm = 'A';
}
else
{
hours -= 12;
pm = 'P';
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.