Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Follow similar conventions for the invalid values of minutes and seconds.
This needs to be done in C++
There needs to be a separate header file for each invalidHr, invalidMin, invalidSec
CODE
SOLUTION
//OUTPUT
//COPYABLE CODE
invalid_Hr.h
#ifndef invalid_Hr_H_
#define invalid_Hr_H_
#include <iostream>
#include <string>
using namespace std;
class invalid_Hr
{
public:
invalid_Hr()
{
msg = "hour between 0 and 12.";
}
invalid_Hr(string str1)
{
msg = str1;
}
string what()
{
return msg;
}
private:
string msg;
};
#endif /* invalid_Hr_H_ */
invalid_Min.h
#ifndef invalid_Min_H_
#define invalid_Min_H_
#include <iostream>
#include <string>
using namespace std;
class invalid_Min
{
public:
invalid_Min()
{
msg = "minutes between 0 and 59.";
}
invalid_Min(string str1)
{
msg = str1;
}
string what()
{
return msg;
}
private:
string msg;
};
#endif /* invalid_Min_H_ */
invalid_Sec.h
#ifndef invalid_Sec_H_
#define invalid_Sec_H_
#include <iostream>
#include <string>
using namespace std;
class invalid_Sec
{
public:
invalid_Sec()
{
msg = "seconds between 0 and 59.";
}
invalid_Sec(string str1)
{
msg = str1;
}
string what()
{
return msg;
}
private:
string msg;
};
#endif /* invalid_Sec_H_ */
main.cpp
#include <iostream>
#include <string>
#include "invalid_Hr.h"
#include "invalid_Min.h"
#include "invalid_Sec.h"
using namespace std;
int get_Hours();
int get_Minutes();
int get_Seconds();
void print_hour(int hr, int min, int sec, string str);
int main ()
{
int hours;
int minutes;
int seconds;
string str;
hours = get_Hours();
minutes = get_Minutes();
seconds = get_Seconds();
cout << "Enter AM or PM: ";
cin >> str;
cout << endl;
cout << "24 hour clock time: ";
print_hour(hours, minutes, seconds, str);
return 0;
}
int get_Hours()
{
bool done = false;
int hr1 = 0;
do
{
try{
cout<<"Enter Hours : ";
cin>>hr1;
if(hr1<0 || hr1>12){
invalid_Hr invalid;
throw invalid;
}
done = true;
}catch(invalid_Hr e){
cout<<e.what()<<endl;
}
}
while (!done);
return hr1;
}
int get_Minutes()
{
bool done = false;
int min1 = 0;
do
{
try{
cout<<"Enter Minutes : ";
cin>>min1;
if(min1<0 || min1>59){
invalid_Min invalid;
throw invalid;
}
done = true;
}catch(invalid_Min e){
cout<<e.what()<<endl;
}
}
while (!done);
return min1;
}
int get_Seconds()
{
bool done = false;
int sec1 = 0;
do
{
try{
cout<<"Enter Seconds : ";
cin>>sec1;
if(sec1<0 || sec1>59){
invalid_Min invalid;
throw invalid;
}
done = true;
}catch(invalid_Min e){
cout<<e.what()<<endl;
}
}
while (!done);
return sec1;
}
void print_hour(int hr1, int min1, int sec1, string str)
{
if (str == "AM")
{
if (hr1 == 12)
cout << 0;
else
cout << hr1;
cout << ":" << min1 << ":" << sec1 << endl;
}
else if (str == "PM")
{
if (hr1 == 12)
cout << hr1;
else
cout << hr1 + 12;
cout << ":" << min1 << ":" << sec1 << endl;
}
}
Get Answers For Free
Most questions answered within 1 hours.