I need help (exceptions: throw, try, catch)

#include <iostream> // For cin, cout, endl, etc.
#include <iomanip> // Output formatting
#include <cstdlib> // Standard library
#include <string> // The string class
#include <cmath> // Standard math functions
#include <fstream> // ifstream, outstream

using namespace std;

// "Time" is the base class
class Time
{
protected:
int hour, min, sec;
public:
// Default Constructor
Time () {
hour = 0;
min = 0;
sec = 0; }
// Parametrized Constructor
Time ( int h, int m, int s ) {
hour = h;
min = m;
sec = s; }
// Accessor Methods
int getHour() const {
return hour; }
int getMin() const {
return min; }
int getSec() const {
return sec; }
};

// "MilTime" is a derived class of "Time"
class MilTime : public Time {
protected:
int milHour, milSec;
public:
// Default Constructor
MilTime() {
hour = 0;
min = 0;
sec = 0;
milHour = 0;
milSec = 0;
}
// Constructor
MilTime ( int tempH, int tempS ) {
if ( tempH <= 1200 ) {
hour = tempH / 100;
min = tempH % 100;
}
else {
hour = ( tempH - 1200 ) / 100;
min = ( tempH - 1200 ) % 100;
}
sec = tempS;
milHour = tempH;
milSec = tempS;
}
void setTIme ( int tempH, int tempS ) {
milHour = tempH;
milSec = tempS;
}
int getHour () {
return milHour;
}
int getStandHr () {
return hour;
}
void badHour ( int tempH ) {
if ( tempH < 0 || tempH > 2359 ) {
throw "Hour is out of range.";
}
}
void badSeconds ( int tempS ) {
if ( tempS < 0 || tempS > 59 ) {
throw "Second is out of range.";
}
}
};

int milH, milS;

int main() {

cout << "Enter hour in millitary format: ";
cin >> milH;

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

MilTime time ( milH, milS );

cout << "Time In Millitary Format: " << time.getHour() << ":" << time.getSec() << endl;

cout << "Time In Standard Format: " << time.getStandHr() << ":" << time.getMin() << ":" << time.getSec() << endl;

return EXIT_SUCCESS;
}

I'm trying to add exceptions (badHour, badSeconds) for entering hours and seconds that are out of range; hour < 0 && hour > 2359 and seconds < 0 && seconds > 59. My program currently asks the user to input the hours (in millitary format) and seconds then displays the time in millitary and standard format. I'm not sure how to use throw, try, and catch. Please help. Thank you.
You have the right idea with your badHour and badSeconds functions, however, I would rename these to checkHour and checkSeconds since the outcome can be either good or bad.

You want to call your check functions from your constructor and from your setter.
1
2
3
4
5
6
7
8
9
10
    MilTime ( int tempH, int tempS ) 
    {	checkHour (tempH);    
	checkSeconds (tempS);
...
    }
    void setTIme ( int tempH, int tempS ) 
    {	checkHour (tempH);
    	checkSeconds (tempS);
...
    }


Now all you need is a try/catch block in main:
1
2
3
4
5
6
7
8
9
10
11
int main() 
{    MilTime time;
...    // prompt for input
    try
        {   time.setTime ( milH, milS );
        }
    catch (std::exception & e)
        {   cout << e.what() << endl;
             return EXIT_FAILURE;
        }
...


As an aside, I find that your storage of two representations of the same information (time) to be awkward. I would have implemented a single representation of time and then converted as needed. If someone calls a setter in the base class, the representation in the derived class is not updated.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.