In: Computer Science
Develop an object-oriented programming (OOP) application to create two clocks simultaneously displaying 12:00 and 24:00 format and allow for user input using secure and efficient C++ code. Thank you!
c++ code for displaying the clock of 12hr and 24 hr formats:
code:
--------------
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
int h,minutes,seconds;
char mer[2];
char *s1="PM",*s2="AM";
cout<<"\nEnter hours:";
cin>>h;
cout<<"\nEnter minutes:";
cin>>minutes;
cout<<"\nEnter seconds:";
cin>>seconds;
cout<<"\nenter meridian:(AM or PM)";
cin>>mer;
cout<<"12-hour format the time is:\n";
cout<<h;
cout<<":"<<minutes<<":"<<seconds<<mer;
if(h<=12 and minutes<=59 and seconds<=59)
{
if(strcmp(mer,s1)==0)
{
h=h+12;
}
else if(strcmp(mer,s2)==0)
{
if(h==12)
h=0;
}
cout<<"\n24-hour format for the asked time is:\n";
cout<<h<<":"<<minutes<<":"<<seconds<<mer;
}
else
cout<<"\ninvalid";
return 0;
}
----------------------------------------------
output:
Enter hours:4
Enter minutes:25
Enter seconds:50
enter meridian:(AM or PM)PM
12-hour format the time is:
4:25:50PM
24-hour format for the asked time is:
16:25:50PM
----------------------------------------------
explanation:
here the time is read from the user in hours, minutes ans seconds format including the meridian.
if meridian==PM then add 12 hours to hours if hours!=12.
if meridian ==Am and hours!=0 make hours as usual.
display both the clock times.
--------------------------------
upvote if you like it.
Thankyou.