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

So I am having to write a program were the user enters time in 12 hour notation.

ID: 3671473 • Letter: S

Question

So I am having to write a program were the user enters time in 12 hour notation. and the program out puts the time in 24 hour notation. the program must contain three exception classes invalidHr, invalidMin, and invalidSec. in the con exception clases there should be two constructors. the default constructor that supplies the standard error message and a parameterized constructor which takes a string variable to intialize the error message. if the user enters any invalid hours, min, or secs the program should throw and catch a error message accordingly. it also says that my derived exception classes should also supply the necesary overloads for returning an error message.

I have to do it in C++

for example user enters 16 22 14 as hours in 12 hour notation the program will display a message saying that it is out of bounds. and if the user enters a char the program outputs a message that tells the user that its not a integer. once the user enters correct values the program will display the time that was entered in 24 hour time. so user enters time in 12 hour and the program will output that time in 24 hour notation

Explanation / Answer

// using standard exceptions
#include <iostream>
#include <exception>
using namespace std;

class myexception: public exception
{
public:
virtual const char* what() const throw()
{
string ex = "";
if(hours_ex) ex += "Hours ";
if(secs_ex) ex += "seconds ";
if(mins_ex) ex += "minutes ";
ex += "Out of bounds";
const char* ret = ex.c_str();
return ret;
}

int hours_ex = 0;
int mins_ex = 0;
int secs_ex = 0;
} myex;

int main () {
int flag = 0;
try
{
cout << "Enter hours: ";
int hours;
cin >> hours;

if( hours < 0 || hours > 12)
myex.hours_ex = flag = 1;

cout << "Enter minutes: ";
int minutes;
cin >> minutes;

if(minutes < 0 || minutes > 60)
myex.mins_ex = flag = 1;

cout << "Enter seconds: ";
int seconds;
cin >> seconds;

cout << "Enter AM/PM: ";
string zone;
cin >> zone;

if(seconds < 0 || seconds > 60)
myex.secs_ex = flag = 1;

if(flag)
throw myex;
else{
if(zone == "PM" || zone == "pm"){
hours += 12;
}

cout << "24-hour format: " << hours << ":" << minutes << ":" << seconds << endl;
}
}
catch (exception& e)
{
cout << e.what() << ' ';
}
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