Question: C++ help: So I am supposed to be building a program that does the following: 1. Do not change anything in the supplied Ch14_Ex4.cpp except
C++ help:
So I am supposed to be building a program that does the following:
1. Do not change anything in the supplied Ch14_Ex4.cpp except to add documentation and your name.
2. Please use the file names listed below since your file will have the following components: Ch14_Ex4.cpp - given file
#include
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; } }
invalidHr.h invalidMin.h invalidSec.h
3. Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation.
a. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec.
b. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object.
c. Similar conventions for the invalid values of minutes and seconds.
So I wrote the program but the exceptions do not quite work. This is because it does not catch them and when I enter an invaild number it just skips through the rest of the program without letting the user re-enter an entry or let them enter anything for the steps not yet gotten to yet. So I would like some help on how to fix this. I am using the int main() that is above and here are my classes:
invaildHr:
#include
#include
using namespace std;
class invalidHr
{
public:
invalidHr() // define message
{
message = "The value of hr must be between 0 and 12.";
}
invalidHr(string str)
{
message = str + "nope";
}
string what() // throw what message object
{
return message;
}
private:
string message;
};
invaildMin:
#include
#include
using namespace std;
class invalidMin
{
public:
invalidMin() // define message
{
message = "The value of min must be between 0 and 59.";
}
invalidMin(string str)
{
message = str + "nope";
}
string what() // return message if what is called
{
return message;
}
private:
string message;
};
invaildSec:
#include
#include
using namespace std;
class invalidSec // define the class
{
public:
invalidSec() // define message
{
message = "The value of sec must be between 0 and 59";
}
invalidSec(string str)
{
message = str + "nope";
}
string what() // return the message if what is called
{
return message;
}
private:
string message;
};
Thank you for any help you can provide!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
