1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
forsale [732]
3 years ago
5

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.
Computers and Technology
1 answer:
Nimfa-mama [501]3 years ago
6 0

Answer:

invalidHr.h

//Implement the header file

#pragma once

#include <iostream>

#include <string>

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

#include <string>

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

#include <string>

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

#include<string>

#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

{

cout << "Enter hours: ";

cin >> hr;

cout << endl;

if (hr < 0 || 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 || min > 59)

throw invalidMin();

done = true;

}

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

{

cout << "Enter seconds: ";

cin >> sec;

cout << endl;

if (sec < 0 || 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;

}

}

Explanation:

You might be interested in
You often travel away from the office. While traveling, you would like to use a modem on your laptop computer to connect directl
34kurt

Answer:

The answer is "Remote access "

Explanation:

The capacity to access another computer or network that you do not have. Remote computer access provides an employee with remote access to the desktop and file. This assists an employee, for example, who works at home efficiently.

Remote users access documents or other resources on any network-connected device or server, enhancing organizational efficiency and increase there are to cooperate more interact with peers nation.

6 0
3 years ago
Sharon is a skilled professional who operates logging equipment.
Viktor [21]
Natural resource systems hope this helps

7 0
3 years ago
Read 2 more answers
Its made up of a small memory chips on a card that can hold data in an electronic format​
valina [46]

The chip can allow you to have more data and more pictures so your phone does not become slow and messages and apps and more.

4 0
4 years ago
If you play video games, please answer these questions it’s for a survey for my game development class!!
yarga [219]

Answer:

Doom, fortnlte, mlnecraft, ark survival evolved, ark survival of the fittest, terraria, raft, among us, ect.

First person shooters, Third person shooters, Creative games

8-10 years

More VR, better graphics, more realistic

Maybe, not anytime soon.

Explanation:

5 0
3 years ago
When using the red / yellow / green method to present status of a project, yellow can mean which of the following?
olga nikolaevna [1]

Answer:

Happiness is one of the obvious ones however I was not given any of the following so I am unsure as to what my options were.

Explanation:

the color is bright and is usually be thought to be the color of the sun which could give us happiness after a rainy day

8 0
4 years ago
Other questions:
  • You may see the term FAQ on websites which stands for Frequently Asked Questions this is an example of which type of mnemonic?
    9·1 answer
  • What runs horizontally and is identified with numbers?
    12·2 answers
  • Turning up the transmit power or utilizing a high gain antenna to reach wireless users from a distance increases your exposure t
    12·1 answer
  • What is one way you can learn about your digital footprint?
    13·1 answer
  • AD and BC are equal perpendiculars to a line segment AB (see figure). Show that CD
    13·1 answer
  • Lifelong learning _____. is only important for professionals with advanced degrees can be formal or informal includes formal cla
    12·1 answer
  • Importing data is sending data to a new file.Is this true or false
    10·2 answers
  • Something I should look for when trying to decide if a source is credible is the publication's ....
    10·1 answer
  • You received an email message stating that your mother's bank account is going to be forfeited if you do not respond to the emai
    6·1 answer
  • Which of these words is used to begin a conditional statement?<br> when<br> input<br> if<br> until
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!