I'm writing a program to display the time of day. Although basic functionality is already done, I'd like the clock to keep updating until a user keystroke. That's why I put in the indefinite while loop.
Here's the source (though it may only work with windows):
Is there a way to make the loop terminate on any keystroke?Code:#include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { while(true) { time_t now; time(&now); cout<<now<<" = "; unsigned int clock = now - 18000; //18000 could be any midnight unsigned int hour = (clock / 3600) % 24; unsigned int minute = (clock % 3600) / 60; unsigned int second = clock % 60; cout<<hour % 12<<":"; if(minute < 10) { cout<<"0"; } cout<<minute<<":"; if (second < 10) { cout<<"0"; } cout<<second; if(hour >= 12) { cout<<" P.M."<<endl; } else { cout<<" A.M."<<endl; } system("cls"); } system("pause"); return 0; }
Is there a better way to make it update so it won't appear flashing?
And...
Is there a better way to clear the screen? (other than system("cls"), which I believe is OS specific.)
Edit: Oh yeah, if you wish to understand my clock, 18000 can be the time_t value of any 12:00 AM, as shown by the comment I just added.