Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that converts 24-hour time to 12-hour time using C++. You will d

ID: 3573332 • Letter: W

Question

Write a program that converts 24-hour time to 12-hour time using C++. You will define an

exception class called TimeFormatMistake. If the user enters an illegal time, like 10:67 or

even gibberish like %9:&5, then your program will throw and catch a TimeFormatMistake

exception.

Class Definition:

- The TimeFormatMistake class must have at least:

o One default constructor,

o One argumented constructor,

o One getter function to retrieve the one data member

o One string data member: message. This data member will be used to display the

following dialog:

“There is no such time as . Try again.”

Sample Output:

Enter time in 24-hour notation: 13:07

That is the same as 1:07 PM

Again (y/n)? y

Enter time in 24-hour notation: 10:15

That is the same as 10:15 AM

Again (y/n)? y

Enter time in 24-hour notation: 10:67

There is no such time as 10:67. Try again.

Enter time in 24-hour notation: %9:&5

There is no such time as %9:&5. Try again.

Enter time in 24-hour notation: 16:05

That is the same as 4:05 PM

Again (y/n)? n

End of Program

Explanation / Answer

#include<iostream.h>

#include<string.h>

#include<conio.h>

class time24

{

int hours,minutes,seconds;

public:

time24()

{

    hours=minutes=seconds=0;

}

time24(int h,int m,int s)

{

    hours=h;

    minutes=m;

    seconds=s;

}

void display()

{

    if(hours<10)

    cout<<'0';

    cout<<hours<<":";

    if(minutes<10)

      cout<<'0';

    cout<<minutes<<":";

    if(seconds<10)

      cout<<'0';

    cout<<seconds;

}

friend class time12;

};

class time12

{

int pm;

int hour,minute;

char *am_pm;

public:

time12(time24);

void display()

{

    cout<<hour<<":";

    if(minute<10)

      cout<<'0';

    cout<<minute<<" ";

    cout<<am_pm;

}

};

time12::time12(time24 t24)

{

int hrs24=t24.hours;

int pm=t24.hours<12 ? 0:1;

int min=t24.seconds<30 ? t24.minutes:t24.minutes+1;

if(min==60)

{

    min=0;

    ++hrs24;

    if(hrs24==12 || hrs24==24)

    pm=(pm==1)? 0:1;

}

int hrs12=(hrs24<13) ? hrs24 : hrs24-12;

if(hrs12==0)

{

    hrs12=12;

    pm=0;

}

    if(pm==1)

    {

      am_pm=" pm";

    }

    else

    {

      am_pm=" am";

    }

    hour=hrs12;

    minute=min;

}

int main()

{

clrscr();

int h1,m1,s1,x=1;

while(x==1)

{

    cout<<"enter 24-hour time: ";

    cout<<"Hours(0-23):";

    cin>>h1;

    if(h1>23)

    return(1);

    cout<<"Minutes:";

    cin>>m1;

    cout<<"Seconds:";

    cin>>s1;

    time24 t24(h1,m1,s1);

    cout<<"you entered:";

    t24.display();

    cout<<endl;

    time12 t12=t24;

    cout<<" 12-hour time: ";

    t12.display();

    cout<<endl;

    x=0;

}

getch();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote