I'm trying to create a timer for fun. I'm running into the trouble of the computer being too fast for counting every second; therefore it prints " cout << "the time" << theTime << endl; " like 10 times, but I want it to only print once. Any suggestions to what I should do or looking into? Thanks.
just thought of a potential solution: using the difftime to check if the new time is different from previous
void activateTimer(int Hours)
{
double seconds;
//current time
//figure out what each lines does http://www.cplusplus.com/reference/ctime/gmtime/
time_t rawtime;
struct tm * ptm;
time(&rawtime);
ptm = localtime(&rawtime);
cout << "The current time is: " << (ptm->tm_hour - 12) << ":" << (ptm->tm_min)<<endl; //time hour time minute
cout << "The stop time is:" << (ptm->tm_hour - 12 + Hours) << ":" << (ptm->tm_min)<<endl;
cout << "Commencing countdown timer." << endl;
while ((ptm->tm_hour - 12) != ((ptm->tm_hour) + (Hours))) //checking if the hours match
{
//declaration
time_t rawtime; //calling the library raw time
struct tm * ptm; //strcuture of 7 things tm_sec, tm_min, tm_hour, pointed by ptm
time(&rawtime); //time=rawtime(null)
ptm = localtime(&rawtime);
if (ptm->tm_sec == 59 || ptm->tm_sec == 60 || ptm->tm_sec == 61)
{
//intializiation
int theTime = ((ptm->tm_hour - 12 + Hours) * 60) + (ptm->tm_min);
cout << "the time" << theTime << endl;
}
}
}