SlideShare a Scribd company logo
1 of 11
Download to read offline
#include
#include
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
using namespace std;
int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, string str);
int main ()
{
int hours;
int minutes;
int seconds;
string str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
cout << "Enter AM or PM: ";
cin >> str;
cout << endl;
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
system("pause");
return 0;
}
int getHours()
{
bool done = false;
int hr = 0;
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 hr;
}
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;
}
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;
}
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;
}
}
invalidHr.h
#include
#include
using namespace std;
class invalidHr
{
public:
invalidHr() // define message
{
message="The value of hr must be between 0 and 12.";
}
invalidHr(string str)
{
message = str + "nope";
}
string what() // throw what message object
{
return message;
}
private:
string message;
};
invalidMin.h
#include
#include
using namespace std;
class invalidMin
{
public:
invalidMin() // define message
{
message="The value of min must be between 0 and 59.";
}
invalidMin(string str)
{
message = str + "nope";
}
string what() // return message if what is called
{
return message;
}
private:
string message;
};
invalidSec.h
#include
#include
using namespace std;
class invalidSec // define the class
{
public:
invalidSec() // define message
{
message="The value of sec must be between 0 and 59";
}
invalidSec(string str)
{
message = str + "nope";
}
string what() // return the message if what is called
{
return message;
}
private:
string message;
};
Solution
#include
#include
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
using namespace std;
int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, string str);
int main ()
{
int hours;
int minutes;
int seconds;
string str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
cout << "Enter AM or PM: ";
cin >> str;
cout << endl;
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
system("pause");
return 0;
}
int getHours()
{
bool done = false;
int hr = 0;
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 hr;
}
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;
}
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;
}
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;
}
}
invalidHr.h
#include
#include
using namespace std;
class invalidHr
{
public:
invalidHr() // define message
{
message="The value of hr must be between 0 and 12.";
}
invalidHr(string str)
{
message = str + "nope";
}
string what() // throw what message object
{
return message;
}
private:
string message;
};
invalidMin.h
#include
#include
using namespace std;
class invalidMin
{
public:
invalidMin() // define message
{
message="The value of min must be between 0 and 59.";
}
invalidMin(string str)
{
message = str + "nope";
}
string what() // return message if what is called
{
return message;
}
private:
string message;
};
invalidSec.h
#include
#include
using namespace std;
class invalidSec // define the class
{
public:
invalidSec() // define message
{
message="The value of sec must be between 0 and 59";
}
invalidSec(string str)
{
message = str + "nope";
}
string what() // return the message if what is called
{
return message;
}
private:
string message;
};

More Related Content

Similar to #include iostream #include string #include invalidHr.h.pdf

Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
mfuentessss
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
seoagam1
 
Military time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdfMilitary time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdf
marketing413921
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
Consider the fork_examplec code under Example code for pr.pdf
Consider the fork_examplec code under Example code for pr.pdfConsider the fork_examplec code under Example code for pr.pdf
Consider the fork_examplec code under Example code for pr.pdf
abinayamobiles
 

Similar to #include iostream #include string #include invalidHr.h.pdf (19)

Ccc
CccCcc
Ccc
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
Military time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdfMilitary time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Consider the fork_examplec code under Example code for pr.pdf
Consider the fork_examplec code under Example code for pr.pdfConsider the fork_examplec code under Example code for pr.pdf
Consider the fork_examplec code under Example code for pr.pdf
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Penjualan swalayan
Penjualan swalayanPenjualan swalayan
Penjualan swalayan
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 

More from APMRETAIL

The start of the European Colonization is typically dated to 1492, a.pdf
The start of the European Colonization is typically dated to 1492, a.pdfThe start of the European Colonization is typically dated to 1492, a.pdf
The start of the European Colonization is typically dated to 1492, a.pdf
APMRETAIL
 
Sewage before being disposed of either in river stream or on land, h.pdf
Sewage before being disposed of either in river stream or on land, h.pdfSewage before being disposed of either in river stream or on land, h.pdf
Sewage before being disposed of either in river stream or on land, h.pdf
APMRETAIL
 
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdfRelational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
APMRETAIL
 
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdfI hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
APMRETAIL
 
Hi Please find my code####### RainFall.java ###################.pdf
Hi Please find my code####### RainFall.java ###################.pdfHi Please find my code####### RainFall.java ###################.pdf
Hi Please find my code####### RainFall.java ###################.pdf
APMRETAIL
 

More from APMRETAIL (20)

The Periodic table of elements are classified through color dependi.pdf
 The Periodic table of elements are classified through color dependi.pdf The Periodic table of elements are classified through color dependi.pdf
The Periodic table of elements are classified through color dependi.pdf
 
option D) answer A and B are both correct .pdf
                     option   D) answer A and B are both correct      .pdf                     option   D) answer A and B are both correct      .pdf
option D) answer A and B are both correct .pdf
 
image not visible clearly. please give points to .pdf
                     image not visible clearly. please give points to .pdf                     image not visible clearly. please give points to .pdf
image not visible clearly. please give points to .pdf
 
HI(g) has the highest molar entropy as it has h.pdf
                     HI(g)  has the highest molar entropy  as it has h.pdf                     HI(g)  has the highest molar entropy  as it has h.pdf
HI(g) has the highest molar entropy as it has h.pdf
 
ion-dipole Solution ion-.pdf
                     ion-dipole  Solution                     ion-.pdf                     ion-dipole  Solution                     ion-.pdf
ion-dipole Solution ion-.pdf
 
Yes. It has SP2 hybridisation , with 2 lone pairs.pdf
                     Yes. It has SP2 hybridisation , with 2 lone pairs.pdf                     Yes. It has SP2 hybridisation , with 2 lone pairs.pdf
Yes. It has SP2 hybridisation , with 2 lone pairs.pdf
 
Using, M1V1 = M2V2 10.0 x 0.1106 = M2 x 13.64 M.pdf
                     Using, M1V1 = M2V2  10.0 x 0.1106 = M2 x 13.64  M.pdf                     Using, M1V1 = M2V2  10.0 x 0.1106 = M2 x 13.64  M.pdf
Using, M1V1 = M2V2 10.0 x 0.1106 = M2 x 13.64 M.pdf
 
O=C=O .pdf
                     O=C=O                                      .pdf                     O=C=O                                      .pdf
O=C=O .pdf
 
The start of the European Colonization is typically dated to 1492, a.pdf
The start of the European Colonization is typically dated to 1492, a.pdfThe start of the European Colonization is typically dated to 1492, a.pdf
The start of the European Colonization is typically dated to 1492, a.pdf
 
The institutions which act as mediator between savers and boorrowers.pdf
The institutions which act as mediator between savers and boorrowers.pdfThe institutions which act as mediator between savers and boorrowers.pdf
The institutions which act as mediator between savers and boorrowers.pdf
 
Solution Mitochondria is known as power house of the cell.It prod.pdf
Solution Mitochondria is known as power house of the cell.It prod.pdfSolution Mitochondria is known as power house of the cell.It prod.pdf
Solution Mitochondria is known as power house of the cell.It prod.pdf
 
Sewage before being disposed of either in river stream or on land, h.pdf
Sewage before being disposed of either in river stream or on land, h.pdfSewage before being disposed of either in river stream or on land, h.pdf
Sewage before being disposed of either in river stream or on land, h.pdf
 
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdfRelational database was proposed by Edgar Codd (of IBM Research) aro.pdf
Relational database was proposed by Edgar Codd (of IBM Research) aro.pdf
 
Option “D” is not true about the net neutrality.Favouring of net n.pdf
Option “D” is not true about the net neutrality.Favouring of net n.pdfOption “D” is not true about the net neutrality.Favouring of net n.pdf
Option “D” is not true about the net neutrality.Favouring of net n.pdf
 
NaF will dissociate 100, and therefore the F- will have extra 0.25 .pdf
NaF will dissociate 100, and therefore the F- will have extra 0.25 .pdfNaF will dissociate 100, and therefore the F- will have extra 0.25 .pdf
NaF will dissociate 100, and therefore the F- will have extra 0.25 .pdf
 
CO2 because the heaviest would have the most attr.pdf
                     CO2 because the heaviest would have the most attr.pdf                     CO2 because the heaviest would have the most attr.pdf
CO2 because the heaviest would have the most attr.pdf
 
Intensity of light source Io = 1000 countsIntensity after absorpti.pdf
Intensity of light source Io = 1000 countsIntensity after absorpti.pdfIntensity of light source Io = 1000 countsIntensity after absorpti.pdf
Intensity of light source Io = 1000 countsIntensity after absorpti.pdf
 
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdfI hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
I hereby explain the SDU (service data unit )and PDU ( protocol data.pdf
 
Hop1 = 13, p2 = 13, and p3 = 13Ha at least one p is not equal.pdf
Hop1 = 13, p2 = 13, and p3 = 13Ha at least one p is not equal.pdfHop1 = 13, p2 = 13, and p3 = 13Ha at least one p is not equal.pdf
Hop1 = 13, p2 = 13, and p3 = 13Ha at least one p is not equal.pdf
 
Hi Please find my code####### RainFall.java ###################.pdf
Hi Please find my code####### RainFall.java ###################.pdfHi Please find my code####### RainFall.java ###################.pdf
Hi Please find my code####### RainFall.java ###################.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

#include iostream #include string #include invalidHr.h.pdf

  • 1. #include #include #include "invalidHr.h" #include "invalidMin.h" #include "invalidSec.h" using namespace std; int getHours(); int getMinutes(); int getSeconds(); void print24HourTime(int hr, int min, int sec, string str); int main () { int hours; int minutes; int seconds; string str; hours = getHours(); minutes = getMinutes(); seconds = getSeconds(); cout << "Enter AM or PM: "; cin >> str; cout << endl; cout << "24 hour clock time: "; print24HourTime(hours, minutes, seconds, str); system("pause"); return 0; } int getHours() { bool done = false; int hr = 0; do {
  • 2. 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 hr; } 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;
  • 3. } } while (!done); return min; } 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; } void print24HourTime(int hr, int min, int sec, string str) { if (str == "AM") { if (hr == 12) cout << 0;
  • 4. else cout << hr; cout << ":" << min << ":" << sec << endl; } else if (str == "PM") { if (hr == 12) cout << hr; else cout << hr + 12; cout << ":" << min << ":" << sec << endl; } } invalidHr.h #include #include using namespace std; class invalidHr { public: invalidHr() // define message { message="The value of hr must be between 0 and 12."; } invalidHr(string str) { message = str + "nope"; } string what() // throw what message object { return message; } private: string message; };
  • 5. invalidMin.h #include #include using namespace std; class invalidMin { public: invalidMin() // define message { message="The value of min must be between 0 and 59."; } invalidMin(string str) { message = str + "nope"; } string what() // return message if what is called { return message; } private: string message; }; invalidSec.h #include #include using namespace std; class invalidSec // define the class { public: invalidSec() // define message { message="The value of sec must be between 0 and 59"; } invalidSec(string str)
  • 6. { message = str + "nope"; } string what() // return the message if what is called { return message; } private: string message; }; Solution #include #include #include "invalidHr.h" #include "invalidMin.h" #include "invalidSec.h" using namespace std; int getHours(); int getMinutes(); int getSeconds(); void print24HourTime(int hr, int min, int sec, string str); int main () { int hours; int minutes; int seconds; string str; hours = getHours(); minutes = getMinutes(); seconds = getSeconds(); cout << "Enter AM or PM: "; cin >> str; cout << endl;
  • 7. cout << "24 hour clock time: "; print24HourTime(hours, minutes, seconds, str); system("pause"); return 0; } int getHours() { bool done = false; int hr = 0; 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 hr; } int getMinutes() { bool done = false; int min = 0; do {
  • 8. 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; } 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;
  • 9. } } while (!done); return sec; } 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; } } invalidHr.h #include #include using namespace std; class invalidHr { public: invalidHr() // define message { message="The value of hr must be between 0 and 12.";
  • 10. } invalidHr(string str) { message = str + "nope"; } string what() // throw what message object { return message; } private: string message; }; invalidMin.h #include #include using namespace std; class invalidMin { public: invalidMin() // define message { message="The value of min must be between 0 and 59."; } invalidMin(string str) { message = str + "nope"; } string what() // return message if what is called { return message; } private: string message; };
  • 11. invalidSec.h #include #include using namespace std; class invalidSec // define the class { public: invalidSec() // define message { message="The value of sec must be between 0 and 59"; } invalidSec(string str) { message = str + "nope"; } string what() // return the message if what is called { return message; } private: string message; };