Write a program using c++ that converts 24-hour time to 12-hour time. You will d
ID: 3573326 • Letter: W
Question
Write a program using c++ that converts 24-hour time to 12-hour time. You will define an
exception class called TimeFormatMistake. If the user enters an illegal time, like 10:67 or
even gibberish like %9:&5, then your program will throw and catch a TimeFormatMistake
exception.
Class Definition:
- The TimeFormatMistake class must have at least:
o One default constructor,
o One argumented constructor,
o One getter function to retrieve the one data member
o One string data member: message. This data member will be used to display the
following dialog:
“There is no such time as <value entered>. Try again.”
Sample Output:
Enter time in 24-hour notation: 13:07
That is the same as 1:07 PM
Again (y/n)? y
Enter time in 24-hour notation: 10:15
That is the same as 10:15 AM
Again (y/n)? y
Enter time in 24-hour notation: 10:67
There is no such time as 10:67. Try again.
Enter time in 24-hour notation: %9:&5
There is no such time as %9:&5. Try again.
Enter time in 24-hour notation: 16:05
That is the same as 4:05 PM
Again (y/n)? n
End of Program
Explanation / Answer
Answer:
/*
Write a program using c++ that converts 24-hour time to 12-hour time. You will define an
exception class called TimeFormatMistake. If the user enters an illegal time, like 10:67 or
even gibberish like %9:&5, then your program will throw and catch a TimeFormatMistake
exception.
Class Definition:
- The TimeFormatMistake class must have at least:
o One default constructor,
o One argumented constructor,
o One getter function to retrieve the one data member
o One string data member: message. This data member will be used to display the
following dialog:
“There is no such time as <value entered>. Try again.” **/
#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime.h"
using namespace std;
void read_hour(istream& ins, int& the_hour) throw (TimeFormatMistake);
void read_minute(istream& ins, int& the_minute);
int digit_to_int(char c) throw (TimeFormatMistake)
{
if ( '0' <= c && c <= '9')
return c - '0';
throw TimeFormatMistake("non digit arg for digit_to_int");
}
DigitalTime::DigitalTime(int the_hour,
int the_minute) throw (TimeFormatMistake)
{
if (the_hour < 0 || the_hour > 23 ||
the_minute < 0 || the_minute > 59)
throw TimeFormatMistake
("Illegal argument to DigitalTime constructor.");
else
{
hour = the_hour;
minute = the_minute;
}
}
DigitalTime::DigitalTime( ) throw()
{
hour = 0;
minute = 0;
}
void DigitalTime::Print12HourTime() throw()
{
enum {AM, PM} am_pm = PM;
int h, m;
if (hour < 12) am_pm = AM;
if (hour == 0 || hour == 12)
h = 12;
else if(hour < 12)
h = hour;
else
h = hour - 12;
cout << h << ":" ;
if (0 <= minute && minute < 10) cout << 0 << minute;
else cout << minute;
if(am_pm == AM) cout << " AM";
else cout << " PM";
}
//Uses iostream:
istream& operator >>(istream& ins, DigitalTime& the_object)
throw (TimeFormatMistake)
{
read_hour(ins, the_object.hour);
read_minute(ins, the_object.minute);
return ins;
}
//Uses iostream:
ostream& operator <<(ostream& outs, const DigitalTime& the_object)
{
outs << the_object.hour << ':';
if (the_object.minute < 10)
outs << '0';
outs << the_object.minute;
return outs;
}
//Uses iostream, cctype, cstdlib, and stdexcept:
void read_hour(istream& ins, int& the_hour) throw(TimeFormatMistake)
{
char c1, c2;
ins >> c1 >> c2;
if (!( isdigit(c1) && (isdigit(c2) || c2 == ':')))
throw TimeFormatMistake
("Non-digit input to read_hour or bad separator ");
if (isdigit(c1) && c2 == ':')
{
the_hour = digit_to_int(c1);
}
else //(isdigit(c1) && isdigit(c2))
{
the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
ins >> c2; //discard ':'
if (c2 != ':')
throw TimeFormatMistake
("Error illegal separating character ");
}
if ( the_hour < 0 || the_hour > 23 )
throw TimeFormatMistake
("Error illegal range for input to read_hour ");
}
//Uses iostream, cctype, stdlib and stdexcept:
void read_minute(istream& ins, int& the_minute)
{
char c1, c2;
ins >> c1 >> c2;
if (!(isdigit(c1) && isdigit(c2)))
throw TimeFormatMistake("Non-digit input to read_minute ");
the_minute = digit_to_int(c1)*10 + digit_to_int(c2);
if (the_minute < 0 || the_minute > 59)
throw TimeFormatMistake
("Error illegal range for input to read_minute ");
}
// Ch16Prog01.cxx
// driver program for Chapter 16 Programming Problem 1.
//
#include "dtime.h"
char getAns()
{
char c;
cin.get(c);
while (c != 'y'&& c != 'n')
{
cout << "y/n, please. ";
cin.get(c);
}
return c;
}
int main()
{
using namespace std;
DigitalTime time24;
char stuff[1002];
char ans;
while( true )
{
cout << "Enter time in 24 hour format: hh:mm ";
try
{
cin >> time24;
cout << "That is the same time as ";
time24.Print12HourTime();
cout << endl;
cout << "Again? (y/n) ";
ans = getAns();
if (ans == 'n') return 0;
}
catch (TimeFormatMistake tfm)
{
cout << tfm.what() << flush;
cin.getline(stuff, 1000); // discard all waiting input
}
}
return 0;
}
Sample output:
Enter time in 24 hour format: hh:mm
13:07
That is the same time as
1:07 PM
Again? (y/n)
y/n, please.
y
Enter time in 24 hour format: hh:mm
10:15
That is the same time as
10:15 AM
Again? (y/n)
y/n, please.
y
Enter time in 24 hour format: hh:mm
10:65
Error illegal range for input to read_minute
Enter time in 24 hour format: hh:mm
16:05
That is the same time as
4:05 PM
Again? (y/n)
y/n, please.
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.