Pause For Some Time

◀ Show How Much Time Has Passed▶ Conversion Between String and Number
Amazon Sometimes you would like your program to halt for some time. Maybe the output fills the screen and you want it to pause for ten seconds, or maybe you write a game in which the user needs to respond within a certain amount of time. In these situations, a function that pauses for some time will be necessary.

A crude function that pauses the program for some time is by using a for loop. Here’s the sample function:
void pauses(){  int pause = 1000000000;  for(int i=0; i<pause; i++)
;}
This function pauses for about seven seconds on my machine. The main advantage of using this approach is that it is very easy to write and to understand. The disadvantage is that you have no explicit control over how long it pauses for. The next function will be much better because you can control how long it pauses for.
#include<ctime>  /* or <time.h> for clock() and clock_t */void pauses2(int sec){	clock_t start = clock(); 	while((float)(clock() - start) / CLOCKS_PER_SEC < sec)
;}

The bad thing about this solution is it'll quickly drain your CPU resources!
Here is another version, but your platform must support the sleep system call.
void pauses3(string sec){	string s = "sleep "+sec;	system(s.c_str());}
◀ Show How Much Time Has Passed▶ Conversion Between String and Number

fShare
Questions? Let me know!