Anyone could help this? I am using a MSVC++ compiler.. Thanks a lot..

Here you go :

#include <iostream>
#include <ctime> //for clock()
 
using std::cout;

void Delay(float milliSeconds)
{
	const float clk_strt = float(clock());
	
	while( clock() - clk_strt  < milliSeconds )
		continue;
}
int main()
{  
	for(int i = 0; i < 10; i++)
	{		
		Delay(1000); //1000 milli seconds = 1 seconds
		cout<< "Time : " << (i+1) << " seconds\n";
	}

	return 0;
}

That takes too much processing and CPU time. Just use the win32 api function Sleep(int seconds), and don't forget to include windows.h header file.

Better would be to use a system sleep function. The looping method will tie up the processor doing needless work (just watch TaskManager while the looping code runs)

In Windows, you can use

#include <iostream>
#include <windows.h>
using namespace std;

int main( )
{
	cout << "sleep" << endl;
	Sleep( 5000 );  //milliseconds
	cout << "awake" << endl;

	return 0;
}

The way I stop the processing is by adding:

char wait;
cout<<"Enter any key to exit";
cin>>wait;

You can put this code anywhere in the program so you can view the output at your own pace.

That takes too much processing and CPU time. Just use the win32 api function Sleep(int seconds), and don't forget to include windows.h header file.

Thanks for the tip. What about portability?

Edited 7 Years Ago by firstPerson: n/a

What about portability?

It is very hard to write useful, fully featured software that has no unportable parts. Knowing when and how to sacrifice portability in a way that benefits the application the most is one of the more important skills, I think. Too bad it is also one of the most difficult.

This article has been dead for over six months. Start a new discussion instead.