Question

In: Computer Science

Project 1 - 24 hour to 12 hour conversion write in c++ Write a program that...

Project 1 - 24 hour to 12 hour conversion

write in c++

Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). 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 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 user says he or she wants to end the program.

Each iteration of the loop will process 3 inputs:

  1. Hour of current time
  2. Minute of current time
  3. Whether or not to continue the loop

You mustimplement 4 functions. You are notpermitted to change the name or signature of these functions; the testing framework is going to directly call the functions. The three functions and their signatures are (use the signatures as a hint for how to implement this!):

  1. get_input(int &hour, int &minute)- function sets the hour and minute based on user input
  2. convert_to_12_hour(int &hour, char &am_pm);- function converts the hour to 12-hour notation and sets AM/PM
  3. print_24_hour_time(int hour, int minute)- function outputs the 24-hour time.
  4. print_12_hour_time(int hour, int minute, char am_pm)- function outputs the 12-hour time.

Sample execution:

Enter the hour in 24-hour format: 9
Enter the minute in 24-hour format: 13
The time in 24-hour format is 09:13
The time in 12-hour format is 09:13AM
Continue? (y/n): y

Enter the hour in 24-hour format: 12
Enter the minute in 24-hour format: 55
The time in 24-hour format is 12:55
The time in 12-hour format is 12:55PM
Continue? (y/n): y

Enter the hour in 24-hour format: 14
Enter the minute in 24-hour format: 05
The time in 24-hour format is 14:05
The time in 12-hour format is 02:05PM
Continue? (y/n): n

Solutions

Expert Solution

input code:

output:

code:

#include <iostream>
#include<iomanip>
using namespace std;
/*get input function*/
void get_input(int &hour, int &minute)
{
/*take input from user and store into variables*/
cout<<"Enter the hour in 24-hour format:";
cin>>hour;
/*take minute input*/
cout<<"Enter the minute in 24-hour format:";
cin>>minute;
}
/*convert to 12 forma*/
void convert_to_12_hour(int &hour,char &am_pm)
{
/*if greater then 12*/
if(hour>11)
{
/*convert to 12*/
if(hour>12)
{
hour=hour%12;
}
else
{
/*for set 12*/
hour=hour;
}
/*set am pm*/
am_pm='P';
}
else
{
hour=hour;
/*set am pm*/
am_pm='A';
}
}
/*print in 24 format*/
void print_24_hour_time(int hour,int minute)
{
/*print*/
cout<<"The time in 24-hour format is "<<setw(2)<<setfill('0') <<hour<<":"<<setw(2)<<setfill('0')<<minute<<endl;
}
/*print in 12 hour format*/
void print_12_hour_time(int hour,int minute,char am_pm)
{
/*if A than print this*/
if(am_pm=='A')
{
cout<<"The time in 24-hour format is "<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<"AM"<<endl;
}
else
{
/*else this*/
cout<<"The time in 24-hour format is "<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<"PM"<<endl;
}
}
int main()
{
/*declare the variables*/
int hour,min;
char am_pm,ch=' ';
do
{
cout<<endl;
/*call the function*/
get_input(hour,min);
print_24_hour_time(hour,min);
convert_to_12_hour(hour,am_pm);
print_12_hour_time(hour,min,am_pm);
/*ask repeat or not*/
cout<<"Continue>(y/n):";cin>>ch;
}while(ch!='n');

return 0;
}


Related Solutions

*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 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. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon. Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon. SAMPLE OUTPUT: Enter time in 24-hour notation: 1614 That is the...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
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 functions: 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;...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
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 functions: 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;...
Write a program in C++ to display the following patterns: * ** *** **** 1 12...
Write a program in C++ to display the following patterns: * ** *** **** 1 12 123 1234 1 22 333 4444 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 * *** ***** ******* ********* ******* ***** *** *
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12...
C++ Program -------------------- Project 5-1: Monthly Sales Create a program that reads the sales for 12 months from a file and calculates the total yearly sales as well as the average monthly sales. Console Monthly Sales COMMAND MENU m - View monthly sales y - View yearly summary x - Exit program Command: m MONTHLY SALES Jan        14317.41 Feb         3903.32 Mar         1073.01 Apr         3463.28 May         2429.52 Jun         4324.70 Jul         9762.31 Aug        25578.39 Sep         2437.95 Oct         6735.63 Nov          288.11 Dec         2497.49...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
I need to write this program in Python. Write a program that displays a temperature conversion...
I need to write this program in Python. Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The tabular format of the result should include rows for all temperatures between 70 and 270 degrees Celsius that are multiples of 10 degrees Celsius (check the sample below). Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit is as follow F=(9/5C) +32 Celsius Fahrenheit 70 158 80 176 90...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT