/* ************************************************************************** * Program name : 060_Class_Time (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2001/02/11 - first version * ************************************************************************** * Description : (a) set the initial time * * (b) get the current time * * (c) Ouput results * * (d) try again ? * ************************************************************************** */ #include #include class Time { // public functions that can be called public: // antwhere in the programe Time (); // Nothing is placed before Time() void getTime (); void printMilitary (); void printStandard (); // private data / function area private: // direct access from the main is NOT allowed int hour; int minute; int second; }; Time::Time() {hour=0; minute=0; second=0; }; void Time::getTime () { cout << "\nEnter the current time 24-hr format : "; cout << "\n\tHour : "; cin >> hour; cout << "\n\tminute : "; cin >> minute; cout << "\n\tsecond : "; cin >> second; if ((hour<0) || (hour >23) || (minute<0) || (minute>59) || (second<0) || (second>59)) { cout << "\nError detected!\n" ; hour = minute = second = 0; } } void Time::printMilitary() { cout << (hour<10?"0":"") << hour << ":" << (minute<10?"0":"") << minute << ":" << (second<10?"0":"") << second << endl; } void Time::printStandard() { cout << ((hour==0||hour==12)? 12:hour%12)<< ":" << (minute<10?"0":"") << minute << ":" << (second<10?"0":"") << second << (hour>12?"PM":"AM") << endl; } int main () { Time t; cout << "\n\nThe initial military time is " ; t.printMilitary (); cout << "\nThe initial standard time is " ; t.printStandard (); char Again; do { t.getTime(); cout << "\n\nThe new military time is " ; t.printMilitary (); cout << "\nThe new standard time is " ; t.printStandard (); cout << "\n\n" << endl; cout << "Try again (Y/N) ? "; cin >> Again; } while ((Again == 'Y') || (Again == 'y')); system("PAUSE"); return 0; }