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

Directions: Write a program that converts 24-hour notation to 12-hour notation.

ID: 3624342 • Letter: D

Question

Directions: Write a program that converts 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. of 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 users says he or she wants to end the program.

Partial code given; just need the start and end section to be filled!

//*****************************************************************************
//
// TwentyFourToTwelve.cpp Authors:
//
// This program prompts the user for a time in 24 hour format as two seperate
// integers. The time is then converted to 12 hour format and displayed to the
// user.
//
//*****************************************************************************

#include <iostream>
using namespace std;

//
// function declarations
//

void input(int& hours, int& minutes);
void output(int hours, int minutes, char type);
int convertTo12Hour(int hours, char& type);

int main()
{
int hours;
int minutes;
char type;
char answer;

do {
input(hours, minutes);
hours = convertTo12Hour(hours, type);
output(hours, minutes, type);

cout << "Perform another calculation? (y/n): ";
cin >> answer;

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

return 0;
} // function main

//
// function definitions
//

void input(int& hours, int& minutes)
{
//
// prompt the user for the number of hours in 24 hour format
//

cout << "Enter the hours for the 24 hour time: ";
cin >> hours;

//
// prompt the user for the number of minutes
//

cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
} // function input


void output(int hours, int minutes, char type)
{
//
// display the output
//

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

//
// special handling for leading 0s on the minutes
//

cout.width(2);
cout.fill('0');
cout << minutes;

//
// Output A.M. or P.M. based on type.
//

if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
} // function output


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------


// --------------------------------
// --------- END USER CODE --------
// --------------------------------



Explanation / Answer

This is the only method you are missing. Put it in and it works like a charm...


int convertTo12Hour(int hours, char& type)
{
type='A';
int toReturn=hours;

if(hours==0||hours==12)
  toReturn=12;

if(hours>12)
 toReturn=hours-12;

if(hours>=12)
 type='P';

return toReturn;
}

 

That's all ! I tested it on Visual C++ and it worked perfectly.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote