Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ (including comments) Write a program to convert the time from 24-hour notati

ID: 3758013 • Letter: C

Question

C++ (including 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

Program code:

// time conversion.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

void inputFormatIn24(int& hrs, int& min);

void inputFormatIn12(int& hrs, int& min, char& format);

void outputFormatIn12(int hrs, int min, char format);

void outputFormatIn24(int hrs, int min);

void convTo12hrFormat(int& hrs, char& format);

void convTo24hrFormat(int& hrs, char& format);

void menu();

int main()

{

int hrs;

int min;

char format;

int choice;

char a;

do

{

menu();//function to diplay choices

cin >> choice;

if (choice == 1)

{

inputFormatIn12(hrs, min, format);///function to get input.

convTo24hrFormat(hrs, format);//function for the conversion.

outputFormatIn24(hrs, min);//function to set output

}

else if (choice == 2)

{

inputFormatIn24(hrs, min);//function to get input.

convTo12hrFormat(hrs, format);///function for the conversion.

outputFormatIn12(hrs, min, format); //function to set output

}

else

{

break;

}

cout << " Do you want to continue?(y/n): ";

cin >> a;

} while ((a == 'Y') || (a == 'y'));

system("PAUSE");

return 0;

}

void inputFormatIn24(int& hrs, int& min)

{

cout << " Enter the hrs for the 24 hour time: ";

cin >> hrs;

cout << " Enter the min for the 24 hour time: ";

cin >> min;

}

// definition of inputFormatIn12 function

void inputFormatIn12(int& hrs, int& min, char& format)

{

cout << " Enter the hrs for the 12 hour time: ";

cin >> hrs;

cout << " Enter the min for the 12 hour time: ";

cin >> min;

cout << " Enter the AM/PM for the 12 hour time(A-AM/P-PM: ";

cin >> format;

}

// // definition of outputFormatIn12 function

void outputFormatIn12(int hrs, int min, char format)

{

cout << " The time converted to 12 hour format is:";

if (format == 'P')

{

if (min < 10) cout << hrs << ":0" << min << " P.M.";

else cout << hrs << ":" << min << " P.M.";

}

else

{

if (min < 10) cout << hrs << ":0" << min << " A.M.";

else cout << hrs << ":" << min << " A.M.";

}

}

// definition of outputFormatIn24 function

void outputFormatIn24(int hrs, int min)

{

cout << " The time converted to 24 hour format is:";

if (hrs < 12)

cout << hrs << ":" << min << " A.M.";

else cout << hrs << ":" << min << " P.M.";

}

// definition of convTo12hrFormat function

void convTo12hrFormat(int& hrs, char& format)

{

if (hrs < 12)

{

format = 'A';

}

else

{

hrs -= 12;

format = 'P';

}

}

// definition of convTo24hrFormat function

void convTo24hrFormat(int& hrs, char& format)

{

if (format == 'P')

{

hrs = hrs + 12;

}

}

void menu()///defintion of menu function

{

cout << "Menu for Time conversion";

cout << " 1.12hrs--24hrs";

cout << " 2.24hrs--12hrs";

cout << " 3.exit";

cout << " Enter your option:";

}

Sample output: