This code is a little weird. Class Time doesn't really store the time, it truncates the time entered to a 12-hour clock inside
setTime()
. Your program carries the "real" hour outside of class Time in
h
, which you then pass to
timeOfDay()
to get "AM", "PM", "noon" or "midnight." If you think about it, this isn't very modular. It would be better if all the work happened inside class Time.
It would be better if the guts of main() did this:
t.setMilitary(h,m); // set time using military format
cout << "civilian time is " << t.civilian() << endl;
cout << "military time is " << t.military() << end;
setMilitary() should take unsigned values instead of int (since negative values are illegal). No need to pass references here. Also, return a bool to indicate whether the values are legal.
Also, notice that you enter the time in main(), but if it's entered wrong then you reprompt for it in setTime(). Ick. Do all the prompting in main.
One more thing: Except for trivial methods, don't put the code and the declaration together. In the real world programmers may want to look at the header file to see exactly what the interface is like and all the code just hides that.
Putting all this together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
class Time {
private:
int hour; // 0-23
int minute; // 0-59
public:
// dmh - always create a default construtor
Time(): hour(0), minute(0)
{;}
bool setMilitary(unsigned h, unsigned m);
string civilian(); // get time in civilian format
string military(); // get time in military format
int getHour() {
return hour;
}
int getMinute() {
return minute;
}
};
bool Time::setMilitary(unsigned h, unsigned m)
{
if (h>23 || m>59) return false;
hour = h;
minute = m;
return true;
}
string Time::civilian()
{
if (minute == 0) {
if (hour == 0) return "midnight";
if (hour == 12) return "noon";
}
ostringstream strm;
if (hour < 13) {
strm << hour;
} else {
strm << hour - 12;
}
strm << ':' << setw(2) << setfill('0') << minute;
if (hour < 12) {
strm << " AM";
} else {
strm << " PM";
}
return strm.str();
}
string Time::military()
{
ostringstream strm;
strm.fill('0');
strm << setw(2) << hour << setw(2) << minute;
return strm.str();
}
int
main()
{
Time t;
int h;
int m;
cout << "Welcome to my military time converter. " << endl;
while (true) {
cout << "\nPlease enter the hour hand. ";
cin >> h;
cout << endl;
cout << "Now enter the minute hand. ";
cin >> m;
cout << endl;
if (t.setMilitary(h, m)) {
break;
} else {
cerr << "\nError, that isn't in standard format. " << endl;
}
}
cout << "Please wait a second. I'm converting it to the correct ";
cout << "format. " << endl;
cout << "\nPress any button to continue. " << endl;
cin.ignore();
cin.get();
cout << "Done " << endl;
cout << "Civilian time is " << t.civilian() << '\n';
cout << "Military time is " << t.military() << '\n';
return 0;
}
|