Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Dec 4, 1998

A pause function

Question:
I cannot figure out a pause function for C++. Our books are very generic...can you please help?

Answer:
To pause for a given amount of time, you will need to interface with your environment. But you didn't indicate what OS your program was running under.

To pause in Windows, you can call the Sleep function, which will put the current thread to "sleep" for the specified number of milliseconds.

If you want to write your own for systems such as DOS, your pause routine will need to sit in a loop until the specified time has elapsed. Something like this:

#include 
#include 

void Pause(int nSeconds)
{
   time_t begin, current;

   // Get start time
   time(&begin);
   // Loop until time has elapsed
   do {
      // Get current time
      time(¤t);
   } while (difftime(current, begin) < nSeconds);
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date