Ask Question
16 April, 12:21

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.

+5
Answers (1)
  1. 16 April, 14:16
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers