I have a problem with my updating clock program. It works fine, except the time is not constantly updating while the program is running. Only once at the launch of the program:
int main ()
{
cout<<"AM/PM Clock"<<endl;
// Declared Variables...
int epochTime;
int epochTimeEST;
epochTime=time(0);
epochTimeEST=epochTime-14400;
int edays1;
int edays2;
int ehours1;
int ehours2;
int eminutes1;
int eminutes2;
int eseconds1;
int ampmHours;
string AMorPM;
edays1=epochTimeEST/86400;
edays2=epochTimeEST%86400;
ehours1=edays2/3600;
ehours2=edays2%3600;
eminutes1=ehours2/60;
eminutes2=ehours2%60;
eseconds1=eminutes2%60;
//Display AM or PM
if (ehours1>=12)
AMorPM="PM";
else
AMorPM="AM";
//Convert from military time
if (ehours1>12)
ampmHours=ehours1-12;
elseif (ehours1==0)
ampmHours=12;
else
ampmHours=ehours1;
while (true) {
cout<<setw(2)<<setfill('0')<<ampmHours<<":"<<setw(2)<<setfill('0')<<eminutes1<<":"<<setw(2)<<setfill('0')<<eseconds1<<AMorPM<<"\r";
}// while
cout<<"Press any key to finish...";
cin.get();
return 0;
} // main()
Your while loop only contains the printing of data.
One thing you have to know is that line 10 (epochTime=time(0);) does not set epochTime equal to "the current time". It does set epochTime equal to the value of the time at moment of execution.
If, at the moment your program executes line 10, time is "1800", then epochTime will be set to 1800. As this is never changed, the "time" variables stay constant during the entire program.
Thus, one vital piece of understanding: in C++, you assign values to variables, not formulas.
To fix it, put all your calculations, starting from epochTime = time(0), inside your while loop, so they are recalculating (i.e. updated) at every iteration.
Do mind: your while loop iterations may execute MUCH faster than the precision of time(), thus it will print the same values for a while until the next second (?) has passed.
Thank you for the reply an thank you for CLEARLY explaining the solution to my problem at why it wasn't working. I assumed that time(0) would constantly update itself, not just at the initialization of the program. I moved the start point of the while statement, and now it works. Thank you very much. I'm also having another issue with a random number generator that this fix will solve also. Have a great day.