Question

Write a program that prompts the user to enter time in 12-hour notation. The program then...

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. Follow similar conventions for the invalid values of minutes and seconds.

This needs to be done in C++

There needs to be a separate header file for each invalidHr, invalidMin, invalidSec

Homework Answers

Answer #1

CODE

SOLUTION

//OUTPUT

//COPYABLE CODE

invalid_Hr.h

#ifndef invalid_Hr_H_

#define invalid_Hr_H_

#include <iostream>

#include <string>

using namespace std;

class invalid_Hr

{

public:

invalid_Hr()

{

msg = "hour between 0 and 12.";

}

invalid_Hr(string str1)

{

msg = str1;

}

string what()

{

return msg;

}

private:

string msg;

};

#endif /* invalid_Hr_H_ */

invalid_Min.h

#ifndef invalid_Min_H_

#define invalid_Min_H_

#include <iostream>

#include <string>

using namespace std;

class invalid_Min

{

public:

invalid_Min()

{

msg = "minutes between 0 and 59.";

}

invalid_Min(string str1)

{

msg = str1;

}

string what()

{

return msg;

}

private:

string msg;

};

#endif /* invalid_Min_H_ */

invalid_Sec.h

#ifndef invalid_Sec_H_

#define invalid_Sec_H_

#include <iostream>

#include <string>

using namespace std;

class invalid_Sec

{

public:

invalid_Sec()

{

msg = "seconds between 0 and 59.";

}

invalid_Sec(string str1)

{

msg = str1;

}

string what()

{

return msg;

}

private:

string msg;

};

#endif /* invalid_Sec_H_ */

main.cpp

#include <iostream>

#include <string>

#include "invalid_Hr.h"

#include "invalid_Min.h"

#include "invalid_Sec.h"

using namespace std;

int get_Hours();

int get_Minutes();

int get_Seconds();

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

int main ()

{

int hours;

int minutes;

int seconds;

string str;

hours = get_Hours();

minutes = get_Minutes();

seconds = get_Seconds();

cout << "Enter AM or PM: ";

cin >> str;

cout << endl;

cout << "24 hour clock time: ";

print_hour(hours, minutes, seconds, str);

return 0;

}

int get_Hours()

{

bool done = false;

int hr1 = 0;

do

{

try{

cout<<"Enter Hours : ";

cin>>hr1;

if(hr1<0 || hr1>12){

invalid_Hr invalid;

throw invalid;

}

done = true;

}catch(invalid_Hr e){

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

}

}

while (!done);

return hr1;

}

int get_Minutes()

{

bool done = false;

int min1 = 0;

do

{

try{

cout<<"Enter Minutes : ";

cin>>min1;

if(min1<0 || min1>59){

invalid_Min invalid;

throw invalid;

}

done = true;

}catch(invalid_Min e){

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

}

}

while (!done);

return min1;

}

int get_Seconds()

{

bool done = false;

int sec1 = 0;

do

{

try{

cout<<"Enter Seconds : ";

cin>>sec1;

if(sec1<0 || sec1>59){

invalid_Min invalid;

throw invalid;

}

done = true;

}catch(invalid_Min e){

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

}

}

while (!done);

return sec1;

}

void print_hour(int hr1, int min1, int sec1, string str)

{

if (str == "AM")

{

if (hr1 == 12)

cout << 0;

else

cout << hr1;

cout << ":" << min1 << ":" << sec1 << endl;

}

else if (str == "PM")

{

if (hr1 == 12)

cout << hr1;

else

cout << hr1 + 12;

cout << ":" << min1 << ":" << sec1 << endl;

}

}

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
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
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...
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...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT