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...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double. Your program should do some basic exception handling: (1) If the file cannot be...
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....
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT