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

#include <iostream>
#include<string>
using namespace std;


void choices()
{
cout<<("1.convert 24hr notation to 12hr notation\n2.1.convert 24hr notation to 12hr notation\n");
cout<<("Enter your choice:");
}

void convert_24hrto12hr(int hr,int min,int sec)
{ cout<<hr<<":"<<min<<":"<<sec<<" in 12hr format is: ";
if(hr>=12)
{if(hr>12)
cout<<hr-12<<":"<<min<<":"<<sec<<"pm";
else
cout<<12<<":"<<min<<":"<<sec<<"pm";
}
else
{
cout<<hr<<":"<<min<<":"<<sec<<"am";
}
}

void convert_12hrto24hr(int hr,int min,int sec, string ampm )
{ cout<<hr<<":"<<min<<":"<<sec<<" "<<ampm<<" in 24hr format is: ";
if(ampm.compare("pm")==0)
{
cout<<hr+12<<":"<<min<<":"<<sec;
}
else
{
cout<<hr<<":"<<min<<":"<<sec;
}
}

int main()
{
choices();
int i;
cin>>i;
if(i==1)
{
int hr,min,sec;
cout<<"Enter hours:";cin>>hr;
cout<<"Enter minutes:";cin>>min;
cout<<"Enter seconds:";cin>>sec;
convert_24hrto12hr(hr,min,sec);

}
else if(i==2)
{
int hr,min,sec;
string ampm;
cout<<"Enter hours:";cin>>hr;
cout<<"Enter minutes:";cin>>min;
cout<<"Enter seconds:";cin>>sec;
cout<<"Enter am / pm:";cin>>ampm;
convert_12hrto24hr(hr,min,sec,ampm );
}
else
{
cout<<"Invalid choice";
}
return 0;
}


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