devxlogo

A pause function

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);}

See also  The Art of AI-Generated Meeting Minutes
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist