Question

Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...

Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM.

(A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below:

void input(int& hours24, int& minutes);

//Precondition: input(hours, minutes) is called with

//arguments capable of being assigned values.

//Postcondition:

// user is prompted for time in 24 hour format:

// HH:MM, where 0 <= HH < 24, 0 <= MM < 60.

// hours is set to HH, minutes is set to MM. The output function will have the converted time displayed as per the declaration below: void output(int hours, int minutes, char AMPM); //Precondition:

// 0 < hours <=12, 0 <= minutes < 60,

// AMPM == 'P' or AMPM == 'A'

//Postcondition:

// time is written in the format

// HH:MM AM or HH:MM PM

A third function performs the actual conversion. 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 declaration or prototype of this third function is as follows:

void convert(int& hours, char& AMPM);

//Precondition: 0 <= hours < 24,

//Postcondition:

// if hours > 12,

// Note: definitely in the afternoon

// hours is replaced by hours - 12,

// AMPM is set to 'P'

// else if 12 == hours

// boundary afternoon hour

// AMPM is set to 'P',

// hours is not changed.

// else if 0 == hours

// boundary morning hour

// hours = hours + 12;

// AMPM = 'A';

// else

// (hours < 12)

// AMPM is set to 'A';

// hours is unchanged

(B) The following “int main()” uses the above three functions for time format conversion. It includes a “do-while” loop that lets the user repeat the time conversion computation for new input values repeatedly until the user says he or she wants to end the program by typing any character other than ‘Y’. Add code to make sure that the following input data conditions are met:

0 <= hours < 24, 0 <= minutes < 60

int main()

{

int hours, minutes;

char AMPM, ans;

do

{

input(hours, minutes);

// Add code to check 0 <= hours < 24, 0 <= minutes < 60

// Otherwise output an ERROR message and break

convert(hours, AMPM);

output(hours, minutes, AMPM);

cout << "Enter Y or y to continue," << " anything else quits." << endl;

cin >> ans;

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

return 0;

}

(C) Develop the definition of the three functions described in part (A).

(D) Run and verify your program. Copy and paste your C++ code from MS Visual Studio:

Copy and paste Console Debug Window results here:

Homework Answers

Answer #1

#include <iostream>
#include <iomanip>

using namespace std;

void input(int& hours24, int& minutes);
void output(int hours, int minutes, char AMPM);
void convert(int& hours, char& AMPM);

int main()
{

int hours, minutes;

char AMPM, ans;

do
{

input(hours, minutes);

// validate hours is between [0, 23] and minutes is between [0,59]
// if not display error message and exit the loop
if(hours < 0 || hours >= 24 && minutes < 0 || minutes >= 60)
{
cout<<"ERROR: Invalid input for time in 24-hour format. Exiting the application."<<endl;
break;
}

convert(hours, AMPM);

output(hours, minutes, AMPM);

cout << "Enter Y or y to continue," << " anything else quits." << endl;

cin >> ans;

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

return 0;

}

//The user provides input as two integers separated by ":"
//Precondition: input(hours, minutes) is called with
//arguments capable of being assigned values.
//Postcondition:
// user is prompted for time in 24 hour format:
// HH:MM, where 0 <= HH < 24, 0 <= MM < 60.
// hours is set to HH, minutes is set to MM.
void input(int& hours24, int& minutes)
{
// input the hours , colon separator in sep and minutes
char sep;
cout<<"Enter the time in 24-hour format(HH:MM): ";
cin>>hours24>>sep>>minutes;
}

//The output function will have the converted time displayed
//Precondition:
// 0 < hours <=12, 0 <= minutes < 60,
// AMPM == 'P' or AMPM == 'A'
//Postcondition:
// time is written in the format
// HH:MM AM or HH:MM PM
void output(int hours, int minutes, char AMPM)
{
// setfill is used to pad the integers with leading 0 so that hours and minutes are displayed as 2 digit integers
// setw(2) is used to set the field width for hours and minutes to 2
cout<<setw(2)<<setfill('0')<<hours<<":"<<setw(2)<<setfill('0')<<minutes<<" ";
if(AMPM == 'P') // PM
cout<<"PM";
else // AM
cout<<"AM";
cout<<endl;
}

//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.
//Precondition: 0 <= hours < 24,
//Postcondition:
// if hours > 12,
// Note: definitely in the afternoon
// hours is replaced by hours - 12,
// AMPM is set to 'P'
// else if 12 == hours
// boundary afternoon hour
// AMPM is set to 'P',
// hours is not changed.
// else if 0 == hours
// boundary morning hour
// hours = hours + 12;
// AMPM = 'A';
// else
// (hours < 12)
// AMPM is set to 'A';
// hours is unchanged
void convert(int& hours, char& AMPM)
{
if(hours < 12) // AM
{
AMPM = 'A';
if(hours == 0) // hours = 0, set hours to 12 in 12-hour format, else hours remains unchanged
{
hours = 12;
}
}
else // PM
{
AMPM = 'P';
if(hours != 12) // hours != 12, set hours to hours-12 to get the hours in 12-hour format, else if hours = 12, then it remains unchanged
hours = hours-12;
}
}

//end of program

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that inputs a time from the console. The time should be in the...
Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should then convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would output “0110 hours” and “12:30 PM” would output “2330 hours”. c++
Write a program that prompts the user to enter time in 12-hour notation. The program then...
Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Follow similar conventions for the invalid values of minutes and seconds. This needs to be done in C++ There needs to be a separate header file for each...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30,...
1. Complete a class Clock that represents time on a 24-hour clock, such as 00:00, 15:30, or 23:59 ○ Time is measured in hours (00 – 23) and minutes (00 – 59) ○ Times are ordered from 00:00 (earliest) to 23:59 (latest) Complete the first constructor of the class Clock ● It takes two arguments: h and m and creates a new clock object whose initial time is h hours and m minutes ● Test cases: Clock clock1 = new...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
// This program prints a table to convert numbers from one unit to another. // The...
// This program prints a table to convert numbers from one unit to another. // The program illustrates some implementation techniques. //Include the header file ostream for including all stream. ---------------------------------------------------------*/ #include <iostream> //Provides cout <v1.0> //Include iomanip that is used for set precision. #include <iomanip> //Provides setw function for setting output width <v1.1> //Header file for EXIT_SUCCESS. #include <stdlib.h>// Provides EXIT_SUCCESS <v1.2> //Header file for assert. #include <assert.h>// Provides assert function <1.3> using namespace std; // Allows all standard...
Hello, in my programming class we have to create a function that will receive a time...
Hello, in my programming class we have to create a function that will receive a time in units of seconds and must return the equivalent time in units of hours-minutes-seconds. The function must also be called from main. I have attached what I have so far if someone could help me finish it that would be great.   int convert(void); int main() {    int time;    time = convert();    printf("Hour:Minute:Seconds is %d\n", time);       return 0; } int...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
A. Write C code to create a structure called time_of_day, which stores the current time in...
A. Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. B. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are...
PLEASE CODE IN C# NOT in Java EXCEPTION HANDLING Concept Summary: Use of try... catch in...
PLEASE CODE IN C# NOT in Java EXCEPTION HANDLING Concept Summary: Use of try... catch in a program Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try... catch and uses an existing exception in C#. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 4. List and describe the THREE (3) necessary conditions for complete similarity between a model and...
    asked 18 minutes ago
  • In C++ Complete the template Integer Average program. // Calculate the average of several integers. #include...
    asked 23 minutes ago
  • A uniform rod is set up so that it can rotate about a perpendicular axis at...
    asked 25 minutes ago
  • To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
    asked 46 minutes ago
  • How could your result from GC (retention time, percent area, etc.) be affected by these following...
    asked 56 minutes ago
  • QUESTION 17 What are the tasks in Logical Network Design phase? (Select five. ) Design a...
    asked 58 minutes ago
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 1 hour ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 1 hour ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 1 hour ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 1 hour ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 1 hour ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 1 hour ago