The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56).
If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one for hours, one for minutes and one for seconds. Each condition should cause a separate exception to be thrown, but they should use a single catch statement.
Ex: If the input is:
6 09 15 56 26 45 13 18 45 03 07 65 45 05 45 18 04 28 75
then the output is:
09:15:56 26 is not a valid hour 18:45:03 65 is not a valid minute 05:45:18 75 is not a valid second
Note, in the event there are multiple errors, only the first error needs to be detected. For example, show below is an example where both hours and seconds were incorrect, but since the exception for for the bad hours values was detected first, there is not exception thrown for the bad seconds value.
Ex: If the input is:
24 13 66
then the output is:
24 is not a valid hour
/*****************************************************/
//Time.h
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
// cin & cout
#include <iomanip>
// I/O formating
using namespace std;
const int BAD_HOUR = 1;
const int BAD_MINUTE = 2;
const int BAD_SECOND = 3;
class StdTime
{
private:
int Hrs;
int Min;
int Sec;
public:
StdTime();
void setTime(int, int, int);
friend ostream&
operator<<(ostream&, const StdTime&);
};
#endif
/******************************************************/
//TimeExcpetions.cpp
#include "Time.h"
int main()
{
int hours;
int minutes;
int seconds;
int count;
StdTime T1;
cin >> count;
while (count-- > 0)
{
cin >> hours >> minutes
>> seconds;
try
{
T1.setTime(hours, minutes, seconds);
cout << T1
<< endl;
}
// insert catch statements here
}
return 0;
}
/******************************************************/
//Time.cpp
#include "Time.h"
using namespace std;
/*** Constructor **************************************/
StdTime::StdTime()
{
Hrs = 0;
Min = 0;
Sec = 0;
}
/******************************************************/
/*** Member function to set time **********************/
void StdTime::setTime(int H, int M, int S)
{
// Insert code that tests the values for H, M, S
// Throws an exception for....
// - Hours (H) not between 0 - 23
// - Minutes (M) not between 0 - 59
// - Seconds (S) not between 0 - 59
//
// If all values (H, M, & S) are legal (i.e. no exceptions
occurred)
// initialize Hrs, Min, and Sec to H, M & S respectively.
}
/******************************************************/
/*** Overload of extraction (<<) operator
*************/
ostream& operator<<(ostream& time_out, const
StdTime& T)
{
cout << setfill('0');
cout << setw(2) << T.Hrs <<
":";
cout << setw(2) << T.Min <<
":";
cout << setw(2) << T.Sec;
return (time_out);
};
/******************************************************/
Compete code in C++:-
#include <bits/stdc++.h>
using namespace std;
class StdTime {
// Private instance variables
private:
int HH, MM, SS;
// Public methods
public:
StdTime();
void setTime(int, int, int);
friend ostream& operator<<(ostream&,
const StdTime&);
};
// Defining constructor
StdTime::StdTime() {
// Initialize all zeros.
this->HH = 0;
this->MM = 0;
this->SS = 0;
}
// Setting time.
void StdTime::setTime(int HH, int MM, int SS) {
// Checking for illegal inputs and throwing
exception.
if(HH < 0 || HH > 23) {
throw to_string(HH)+" is not a
valid Hour\n";
}
if(MM < 0 || MM > 59) {
throw to_string(MM)+" is not a
valid Minute\n";
}
if(SS < 0 || SS > 59) {
throw to_string(SS)+" is not a
valid Second\n";
}
this->HH = HH;
this->MM = MM;
this->SS = SS;
}
// Operator overloading.
ostream& operator<<(ostream& time_out, const
StdTime& T)
{
cout << setfill('0');
cout << setw(2) << T.HH << ":";
cout << setw(2) << T.MM << ":";
cout << setw(2) << T.SS;
return (time_out);
};
// Main function
int main(void) {
// local variables for user inputs.
int hours, minutes, seconds, count;
// Creating instance of class 'StdTime'.
StdTime T1;
// Taking input the number of times.
cin >> count;
// Taking time inputs.
while (count-- > 0)
{
cin >> hours >> minutes >>
seconds;
try
{
T1.setTime(hours, minutes, seconds);
cout << T1 << endl;
} catch(const string msg) {
cerr <<
msg;
}
}
return 0;
}
Screenshot of output:-
Get Answers For Free
Most questions answered within 1 hours.