Ask Question
28 September, 19:07

Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Similar conventions for the invalid values of minutes and seconds.

+1
Answers (1)
  1. 28 September, 21:18
    0
    invalidHr. h

    //Implement the header file

    #pragma once

    #include

    #include

    using namespace std;

    //Declare class and thrown an exception

    class invalidHr : public exception

    {

    //Implement the member function

    public:

    virtual const char * what () const throw ()

    {

    //return the message

    return "The value of hr must be between 0 and 12.";

    }

    };

    invalidMin. h

    #pragma once

    //Implement the header file

    #include

    #include

    using namespace std;

    //Declare class and thrown an exception

    class invalidMin : public exception

    {

    //Implement the member function

    public:

    virtual const char * what () const throw ()

    {

    //return the message

    return "The value of minutes must be between 0 and 59.";

    }

    };

    invalidSec. h

    //Implement the header file

    #pragma once

    #include

    #include

    using namespace std;

    //Declare class and thrown an exception

    class invalidSec : public exception

    {

    //Implement the member function

    public:

    virtual const char * what () const throw ()

    {

    //return the message

    return "The value of seconds must be between 0 and 59.";

    }

    };

    Driverclass. cpp:

    //include header files

    #include

    #include

    #include "invalidHr. h"

    #include "invalidMin. h"

    #include "invalidSec. h"

    using namespace std;

    //function prototypes

    int getHours ();

    int getMinutes ();

    int getSeconds ();

    void print24HourTime (int hr, int min, int sec, string str);

    //Main method

    int main ()

    {

    //Declare variables

    int hours;

    int minutes;

    int seconds;

    string str;

    //call methods to get the hours, minutes and seconds

    hours = getHours ();

    minutes = getMinutes ();

    seconds = getSeconds ();

    //Prompt and read AM or PM

    cout << "Enter AM or PM: ";

    cin >> str;

    cout << endl;

    //Print the output line

    cout << "24 hour clock time: ";

    //call the methods to print the time

    print24HourTime (hours, minutes, seconds, str);

    system ("pause");

    return 0;

    }

    //Implement the method hetHours ()

    int getHours ()

    {

    //declare bool flag

    bool done = false;

    //declare hours

    int hr = 0;

    //Using do-while loop and try catch block

    //to repeat the loop until user enters valid hours.

    //Otherwise thown an exception

    do

    {

    try

    hr > 12)

    throw invalidHr ();

    done = true;

    catch (invalidHr hrObj)

    {

    cout << hrObj. what () << endl;

    }

    } while (! done);

    //return hours

    return hr;

    }

    //Using do-while loop and try catch block

    //to repeat the loop until user enters valid minutes.

    //Otherwise thown an exception

    int getMinutes ()

    {

    bool done = false;

    int min = 0;

    do

    {

    try

    cout << "Enter minutes: ";

    cin >> min;

    cout << endl;

    if (min < 0

    catch (invalidMin minObj)

    {

    cout << minObj. what () << endl;

    }

    } while (! done);

    return min;

    }

    //Using do-while loop and try catch block

    //to repeat the loop until user enters valid seconds.

    //Otherwise thown an exception

    int getSeconds ()

    {

    bool done = false;

    int sec = 0;

    do

    {

    try

    sec > 59)

    throw invalidSec ();

    done = true;

    catch (invalidSec secObj)

    {

    cout << secObj. what () << endl;

    }

    } while (! done);

    return sec;

    }

    //Implement the methods to print the hours minutes and seconds

    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;

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers