The sleep function...

This is a discussion on The sleep function... within the C++ Programming forums, part of the General Programming Boards category; Rite, ive heard about the sleep function, but I have no idea on how to use it in a program, ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question 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???


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    In linux it's sleep(1) (notice the lower case s). There argument here is in seconds - not milliseconds like windows.

  4. #4
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    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...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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...

  7. #7
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    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.
    Last edited by DeepFyre; 09-07-2004 at 04:23 PM.
    Keyboard Not Found! Press any key to continue. . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21