Question

The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...

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);
};
/******************************************************/

Homework Answers

Answer #1

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:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class called time that has three data members hours, minutes, and seconds. The class...
Create a class called time that has three data members hours, minutes, and seconds. The class has the following member functions: - Overloaded Constructors - Display function to display it, in 11:59:59 format. - SetTime() - IsValid(): Check if the time is valid or not - AddTime(Time t1, Time t2): Add two time values passed as a parameter leaving the result in third time variable. - Increment(): Tells what will be time after a second - Decrement(): Tells what was...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
Time Remaining 2 hours 59 minutes 15 seconds 02:59:15 Item 1 Item 1 Time Remaining 2...
Time Remaining 2 hours 59 minutes 15 seconds 02:59:15 Item 1 Item 1 Time Remaining 2 hours 59 minutes 15 seconds 02:59:15 ProForm acquired 80 percent of ClipRite on June 30, 2020, for $1,520,000 in cash. Based on ClipRite's acquisition-date fair value, an unrecorded intangible of $500,000 was recognized and is being amortized at the rate of $17,000 per year. No goodwill was recognized in the acquisition. The noncontrolling interest fair value was assessed at $380,000 at the acquisition date....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT