devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

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.