Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I am having to write a program were the user enters time in 12 hour notation.

ID: 3671300 • Letter: S

Question

So I am having to write a program were the user enters time in 12 hour notation. and the program out puts the time in 24 hour notation. the program must contain three exception classes invalidHr, invalidMin, and invalidSec. in the con exception clases there should be two constructors. the default constructor that supplies the standard error message and a parameterized constructor which takes a string variable to intialize the error message. if the user enters any invalid hours, min, or secs the program should throw and catch a error message accordingly.

I have to do it in C++

Explanation / Answer

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

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.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;
   }


output

Please enter a time in 12-Hour notation:                                                                                                                    
13                                                                                                                                                          
14                                                                                                                                                          
14                                                                                                                                                          
15                                                                                                                                                          
13 is an invalid number for the number of hours.                                                                                                            
Please enter a time in 12-Hour notation:                                                                                                                    
24                                                                                                                                                          
13                                                                                                                                                          
14                                                                                                                                                          
This time in 24-hour notation is:                                                                                                                           
5:4:3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote