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

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

#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; };