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...
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
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT