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 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...
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...
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...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
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...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT