Hi, the following program runs fine with Borland. It is a twelve hour clock. What it does is requests hours, mins, secs and then waits for user input. When user hits a key the program stops running and adds the elapsed time to the user input time and outputs it.
There are two issues when i compile using gcc.
(1) getch() is not avilable in GCC, but i need a simple substituition that will permit a pause for user input(press any key ....)
(2) The time.h file, i am not sure if its is right. I cannot even get to the output it just skips past when user input is required.
Please can you possibly help me, I need this to work for me to go ahead with the world clock!
PS: Excuse the code; I am just learning, any help would be appreciated.
Code://program: 12 hour time clock for c++ #include<iostream> #include<time.h> #include<conio.h> class myClock{ private: int hh, mm, ss; int start, timerSecs; //for the timer public: myClock(); //constructor void get_time(int h, int m, int s); void startTimer(); void endTimer(); void add_time(); void show_time(); }; myClock :: myClock() { hh = mm = ss = 0; } void myClock :: get_time(int h, int m, int s) { hh = h; mm = m; ss = s; } void myClock :: startTimer() { start = clock(); //get the initial clock position } void myClock :: endTimer() { clock_t end; //to get the final clock position end = clock(); timerSecs = (end - start)/CLK_TCK; // to get value in secs } void myClock :: add_time() { ss += timerSecs; if ( ss > 59 ) { mm ++; ss %= 60; } if ( mm > 59 ){ hh ++; mm %= 60; } if ( hh > 12 ){ hh %= 12; } } void myClock :: show_time() { cout << "The time is: " << hh << ":" << mm << ":" << ss; } int main() { int hours, mins, secs; myClock c; cout << "Enter the hours: "; cin >> hours; cout << "Enter the minutes: "; cin >> mins; cout << "Enter the seconds: "; cin >> secs; c.get_time(hours, mins, secs); cout << "c is now ticking ..." << "\n"; c.startTimer(); cout << "Press any key to stop c "; getch(); c.endTimer(); c.add_time(); c.show_time(); return 0; }
[code][/code]tagged by Salem



LinkBack URL
About LinkBacks


