-
The sleep function...
Rite, ive heard about the sleep function, but I have no idea on how to use it in a program, so can some one please tell me everything to do with the sleep function (To tell you the truth, im not even sure that it exists), but anyway, can some1 help me???
:)
-
Which sleep function? That operation is nonstandard, so there are several variations of it. Here is one way to use one of them (the Sleep function from windows.h):
Code:
#include <iostream>
#include <string>
#include <windows.h>
int main()
{
typedef std::string::const_iterator str_iter;
const std::string msg ( "This is a test message" );
for ( str_iter it = msg.begin(); it != msg.end(); ++it ) {
std::cout.put ( *it );
Sleep ( 100 );
}
std::cout<<std::endl;
}
The argument takes a time slice in millisecond increments, so 100 is 1/10 of a second.
-
In linux it's sleep(1) (notice the lower case s). There argument here is in seconds - not milliseconds like windows.
-
Is there a variation that can be used more for some basic "animation", where a basic word pattern is shown for a few seconds, then for a split second disappears and then re-appears a few lines down, I no this might sound a bit strange, and a bit of a tall order, so it wouldn't surprise me if u told me that there was no way of doing it unless you used a more complex function...
-
Yes, you can do that. But you'll need a way to pause the program briefly, a way to clear the old text, and possibly even a way to place text at an arbitrary location on the screen. Check the FAQ for possible solutions to each of those.
-
Why can't you you use this one? Just do:
Code:
for (int i = 0; i < 25; i++)
{
Draw(Pattern.bmp, i, 10); // Just an example - you'd use a graphics API for this
Sleep(10);
}
edit: Having just read Prelude's response, I'll add this: You can use Sleep to do animation but there's more to animation than that. What I would've included in my "Draw" function would be all that stuff: clearing the screen, etc...
-
std::cout << " the text " << endl; // the text that is going to show
sleep ( 50 ); // pause for half a second
clrcr();
sleep ( 50 ); // pause for half a second
std::cout << " the text " << endl;
I think that would work, but I am just a novice programmer so you might have to check it with someone else. You would have to include iostream, windows.h, conio.h and if that doesnt work (not sure what the criteria for this is) you might have to put it in a project file and add conio.c to the project file.
Someone correct me if I am wrong on any of this.
Oh and sorry for the sloppy code...I still have to figure out how to do it right. :(