Question

In: Computer Science

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; and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.) c++

Solutions

Expert Solution

C++ Program:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

//Function prototypes
void get_input(int &hour, int &minute); //- function sets the hour and minute based on user input
void get_12_input(int &hour, int &minute, string &am_pm); //- function sets the hour and minute based on user input
void convert_to_12_hour(int &hour, int &minu, char &am_pm); //- function converts the hour to 12-hour notation and sets AM/PM
void convert_to_24_hour(int &hour, int &minu, string zone); //- function converts the hour to 24-hour notation
void print_24_hour_time(int hour, int minute); //- function outputs the 24-hour time.
void print_12_hour_time(int hour, int minute, char am_pm); //- function outputs the 12-hour time.
void menu(); //Prints the menu

//Main function
int main()
{
int h, m;
int h24, m24;
char AM_PM;
string zone;
int ch;

//Menu
menu();
cin >> ch;

//Loop till user want to quit
while(ch != 3)
{
switch(ch)
{
case 1: //Reading input
get_12_input(h, m, zone);

//Conversion
convert_to_24_hour(h, m, zone);

h24 = h;
m24 = m;

//Printing
print_24_hour_time(h24, m24);
break;

case 2: //Reading input
get_input(h24, m24);
//Assigning values
h = h24;
m = m24;

//Conversion
convert_to_12_hour(h, m, AM_PM);

//Printing
print_12_hour_time(h, m, AM_PM);
break;
}

//Menu
menu();
cin >> ch;
}

return 0;
}

//Menu
void menu()
{
cout << "\n ***** MENU ***** \n 1 - 12Hr to 24Hr \n 2 - 24Hr to 12Hr \n 3 - Quit \n Your selection: ";
}

//Function that reads a line of text from file
void get_input(int &h, int &m)
{
//Reading a line of input file
cout << "\nEnter the hour in 24-hour format: ";
cin >> h;

cout << "\nEnter the minute in 24-hour format: ";
cin >> m;
}

//Function that reads a line of text from file
void get_12_input(int &h, int &m, string &am_pm)
{
//Reading a line of input file
cout << "\nEnter the hour in 12-hour format: ";
cin >> h;

cout << "\nEnter the minute in 12-hour format: ";
cin >> m;

cout << "\nEnter Time Zone: ";
cin >> am_pm;
}

//Function that converts 24 hr format to 12 hr format
void convert_to_12_hour(int &h, int &m, char &AM_PM)
{
//Converting time
if(h == 12)
{
AM_PM = 'P';
}
else if(h > 12)
{
h = h-12;
AM_PM = 'P';
}
else
{
AM_PM = 'A';
}
}

//Function that converts 12 hr format to 24 hr format
void convert_to_24_hour(int &h, int &m, string AM_PM)
{
//Converting time
if(AM_PM == "PM")
{
h = h+12;
}
}


//Function that prints the time format
void print_24_hour_time(int h, int m)
{
cout << "\nThe time in 24-hour format is ";
cout << setfill('0') << setw(2) << h << ":" << setfill('0') << setw(2) << m << "\n";
}

//Function that prints the time format
void print_12_hour_time(int h, int m, char AM_PM)
{
cout << "\nThe time in 12-hour format is ";
//If hours == 12
if(h == 12)
cout << setfill('0') << setw(2) << h << ":" << setfill('0') << setw(2) << m << " " << ((AM_PM=='A')?"AM":"PM") << " \n";
//Hours > 12
else if(h > 12)
cout << setfill('0') << setw(2) << (h-12) << ":" << setfill('0') << setw(2) << m << " " << ((AM_PM=='A')?"AM":"PM") << " \n";
//Hours < 12
else
cout << setfill('0') << setw(2) << h << ":" << setfill('0') << setw(2) << m << " " << ((AM_PM=='A')?"AM":"PM") << " \n";
}

_________________________________________________________________________________________

Sample Run:


Related Solutions

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;...
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...
*** 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...
1. Explain how to convert positive integers from base 3 to base 10 and vice versa....
1. Explain how to convert positive integers from base 3 to base 10 and vice versa. Include examples. 2. Explain how to convert fractions from base 3 to base 10 and vice versa. Include examples.
Convert from rectangular to polar form and vice versa: a. ?6 b. 2√5∠135∘ c. −2 −...
Convert from rectangular to polar form and vice versa: a. ?6 b. 2√5∠135∘ c. −2 − ?2√3 d. √2∠45∘ Please show your steps because I need to understand how we reached the solution Thank you
ConvertingDecimalValuesintoBina ry,andViceVersa. PartA Being able to convert decimal values to binary (and vice versa) is very...
ConvertingDecimalValuesintoBina ry,andViceVersa. PartA Being able to convert decimal values to binary (and vice versa) is very important in networking because this is the basis for how subnetting is done. You may have done some of these exercises in high school and probably didn’t know why it was important to be able to convert decimal values into binary, and vice versa. This hands-on activity will help yourecallhowthisisdoneorwillteachhowtodoitincase youneverseenthisbefore. 152 Chapter 5 Network and Transport Layers As you know, an IPv4 address...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and...
Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and 24-hour time (military time). We will assume in the context there is no need for seconds, so time12 use only hours (from 1 to 12), minutes, and an "a.m." or "p.m." designation. Our time24 class, which uses hours (from 00 to 23), minutes and seconds. 12-Hours Time 24-Hours Time 12:00 a.m. (midnight) 00:00 12:01 a.m. 00:01 12:00 p.m. 12:00 6:00 p.m. 18:00 11:59 p.m....
Write a Bash script clock.sh that once/second will print the time in the 24-hour clock format...
Write a Bash script clock.sh that once/second will print the time in the 24-hour clock format HH:MM:SS. See "man date" for a command to produce this format of time. rubric clock.sh prints h:m:s in 24-hour clock format clock.sh prints time 1/second
How do things get from the neuronal cell body to the presynaptic terminal, and vice versa?...
How do things get from the neuronal cell body to the presynaptic terminal, and vice versa? Describe / explain / discuss any molecules and/or cellular structures involved
Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM)...
Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM) and convert it to its equivalent 24-hour format (e.g. 2305). Assume 0000 in 24-hour format is 12:00AM in 12-hour format. The program should continue accepting inputs until a sentinel value of 99:99AM is entered. Ex: If the input is: 09:04AM 10:45AM 11:23AM 99:99AM IN Java please, Thanks the output is: 0904 1045 1123
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT