Which file to include using sleep()?
Hi friends,
I am last year computer science student. I recently started learning c++ language. In one of my code I tried to use sleep() method to make my program to take a break, but I don't know which file to include using sleep()?. I tried to use <stdlib.h>, but it doesn't work. Can anyone help me to fix this problem.
Thanks in advanced.
Re: Which file to include using sleep()?
Hey you have use wrong header in your code and that's why you are getting such type of problem. You have use "stdlib.h" instead of "windows.h" and that's why your program is not running. In this case use "windows.h" in your header. You also have use make one silly mistake, your function start with a small "s". Just make it capital like Sleep(1500) to fix this problem.
Re: Which file to include using sleep()?
Hey you have to use "time.h" file in your sleep() function to get rid out of this problem. I have written following code for you. Just try to understand this. It is very simple code. I have pass one object known as "times_ts delay" to sleep() function to fix this problem.
Code:
void sleep( times_ts delay )
{
times_ts timers0, timers1;
times( &timers0 );
do
{
times( &timers1 );
}
while (( timers1 - timers0 ) < delay );
}
Re: Which file to include using sleep()?
The sleep() function is used to suspends execution for an interval. When you call sleep() function in your program, the current running program is suspended from execution for the few seconds which is specified by the argument seconds. I have written following program using sleep() function. Just try to understand this.
Code:
#include <dos.h>
#include <stdio.h>
#include <conio.h>
int main()
{
printf("Messages are 1\n");
sleep(3);
printf("Message "welcome" three seconds after Message "End" ");
getch();
return 0;
}
Re: Which file to include using sleep()?
As per my knowledge sleep() function is used to suspend execution of an program for an interval of time. To use this function you have to first import "unistd.h" header in your program and after this you have to use sleep() function. You can do this in following ways.
Code:
#include <unistd.h>
unsigned sleep(unsigned seconds1);
Re: Which file to include using sleep()?
I have written following code for you using sleep() function. This is very simple code. Just try to understand this code. In the following code I have use iostream class to include all input and output method. I also have use windows.h class to use sleep() function.
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int k;
for(k=0; k<1000+1; k+=1)
{
system("CLSEG");
cout << k << endl;
Sleep(1000);
}
return 0;
}