Write a program that prompts the user to enter time in 12-hour notation. The pro
ID: 3675265 • Letter: W
Question
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. Similar conventions for the invalid values of minutes and seconds.
How would I create invalidHr.h, invalidMin.h, and invalidSec.h to have it work with my code below?
//main
#include <iostream>
#include <string>
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
using namespace std;
int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, string str);
int main ()
{
int hours;
int minutes;
int seconds;
string str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
cout << "Enter AM or PM: ";
cin >> str;
cout << endl;
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
system("pause");
return 0;
}
int getHours()
{
bool done = false;
int hr = 0;
do
{
try
{
cout << "Enter hours: ";
cin >> hr;
cout << endl;
if (hr < 0 || hr > 12)
throw invalidHr();
done = true;
}
catch (invalidHr hrObj)
{
cout << hrObj.what() << endl;
}
}
while (!done);
return hr;
}
int getMinutes()
{
bool done = false;
int min = 0;
do
{
try
{
cout << "Enter minutes: ";
cin >> min;
cout << endl;
if (min < 0 || min > 59)
throw invalidMin();
done = true;
}
catch (invalidMin minObj)
{
cout << minObj.what() << endl;
}
}
while (!done);
return min;
}
int getSeconds()
{
bool done = false;
int sec = 0;
do
{
try
{
cout << "Enter seconds: ";
cin >> sec;
cout << endl;
if (sec < 0 || sec > 59)
throw invalidSec();
done = true;
}
catch (invalidSec secObj)
{
cout << secObj.what() << endl;
}
}
while (!done);
return sec;
}
void print24HourTime(int hr, int min, int sec, string str)
{
if (str == "AM")
{
if (hr == 12)
cout << 0;
else
cout << hr;
cout << ":" << min << ":" << sec << endl;
}
else if (str == "PM")
{
if (hr == 12)
cout << hr;
else
cout << hr + 12;
cout << ":" << min << ":" << sec << endl;
}
}
Explanation / Answer
main.cpp
#include <iostream>
#include "invalidTimes.h"
using namespace std;
void const printTime(const int& hour, const int& minute, const int& second);
//Function to print the time
int main()
{
int *hr = NULL, *min = NULL, *sec = NULL;
//Pointers to hold the users input
char *midday;
//Variable to hold if am or pm and used to throw away extra charters
bool correct = false;
//Variable to break while loop that checks input
hr = new int;
min = new int;
sec = new int;
midday = new char;
//Dynamically creates the variables to track the users input
while(!correct)
{
cout << "Please enter a time in 12-Hour notation: " << endl;
cin >> *hr >> *midday >> *min >> *midday >> *sec >> *midday;
//Takes in the users input
try
{
if(*hr > 12 || *hr < 1)
//Tests if hr is within an acceptable range
throw invalidHr(*hr);
if(*min >= 60 || *min < 0)
//Tests if min is within an acceptable range
throw invalidMin(*min);
if(*sec >= 60 || *sec < 0)
//Tests if hr is within an acceptable rangeS
throw invalidSec(*sec);
correct = true;
}
catch(invalidHr& invalidH)
{
invalidH.fix();
}
catch(invalidMin& invalidM)
{
invalidM.fix();
}
catch(invalidSec& invalidS)
{
invalidS.fix();
}
catch(...)
{
cout << "Whoops, something went wrong." << endl;
}
}
if(*midday == 'p')
//If the time is past midday then add 12 to put in 24-hour notation
*hr += 12;
cout << "This time in 24-hour notation is:" << endl;
printTime(*hr, *min, *sec);
//Prints the result of the previous calculation
return 0;
}
void const printTime(const int& hour, const int& minute, const int& second)
{
cout << hour << ":" << minute << ":" << second;
//Outputs time in formatted format
}
invalidTimes.h
#ifndef INVALIDTIMES
#define INVALIDTIMES
class invalidHr
{
public:
void fix();
//Function to tell user of the incorrect time
invalidHr();
//default constructor
invalidHr(int);
//Constructor with the int to set variable
private:
int *hr;
//Pointer to track the int
};
class invalidMin
{
public:
void fix();
//Function to tell user of the incorrect time
invalidMin();
//default constructor
invalidMin(int);
//Constructor with the int to set variable
private:
int *min;
//Pointer to track the int
};
class invalidSec
{
public:
void fix();
//Function to tell user of the incorrect time
invalidSec();
//default constructor
invalidSec(int);
//Constructor with the int to set variable
private:
int *sec;
//Pointer to track the int
};
#endif
invalidTimesImpl.cpp
#include <iostream>
#include "invalidTimes.h"
using namespace std;
//======================================= for invalidHr class ===========================================
void invalidHr::fix()
//Function to tell user of the incorrect time
{
cout << *hr << " is an invalid number for the number of hours." << endl;
}
invalidHr::invalidHr()
//default constructor
{
hr = new int;
*hr = 0;
}
invalidHr::invalidHr(int num)
//Constructor with the int to set variable
{
hr = new int;
*hr = num;
}
//================================================= invalidMin =======================================
void invalidMin::fix()
//Function to tell user of the incorrect time
{
cout << *min << " is an invalid number for the number of minutes." << endl;
}
invalidMin::invalidMin()
//default constructor
{
min = new int;
*min = 0;
}
invalidMin::invalidMin(int num)
//Constructor with the int to set variable
{
min = new int;
*min = num;
}
//================================================ invalidSec =========================================
void invalidSec::fix()
//Function to tell user of the incorrect time
{
cout << *sec << " is an invalid number for the number of seconds." << endl;
}
invalidSec::invalidSec()
//default constructor
{
sec = new int;
*sec = 0;
}
invalidSec::invalidSec(int num)
//Constructor with the int to set variable
{
sec = new int;
*sec = num;
}