Thread: Sleep

  1. #1
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469

    Question Sleep

    When I run this code:
    Code:
    #include <windows.h>
    #include <iostream.h>
    
    int sleep (int sleep_for)
    {
    	int start=timeGetTime();
    	BOOL sleeping=TRUE;
    	while (sleeping)
    	{
    		int current=timeGetTime();
    		if (current-sleep_for>=start)
    		break;
    	}
    	return 0;
    }
    
    int main()
    {
    	cout<<"YAY";
    	sleep(2000);
        return 0;
    }
    it sleep for the right amount of time, but it doesn't display the text. Why isn't the text being displayed? Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cout<<"YAY";
    Try:
    cout<<"YAY" << flush;

  3. #3
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Thanks, that works. But why should it work? I've never seen flush before.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    On some compilers, the output isn't sent to the console until a newline (endl) is written, or maybe the buffer fills up. So you have to flush the stream to force it to be written to the console. I use a borland compiler, and the flush usually isn't needed.

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    I use VC++ .NET and flush usually isn't need either

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    15
    what type of project is it? i tired win32 app and console app it won't compile an exe file >_<
    Dude, your getting a dell!

  7. #7
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Yeah, you need to have the winmm.lib included. Right click on the project in solution explorer. Go to properties and choose linker. It's the second thing down in the linker folder called Input. Go to the first thing on the input tab where it says additional dependecies and type "winmm.lib" lose the quotes though. Then go to the bottom and hit apply.

    PS- It's a console app

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    15
    i really dislike to be such a pain, whats solution explorer?

    I am useing Visual C++ 5.0/97 if it helps any
    Dude, your getting a dell!

  9. #9
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    oh, I was using .NET. In your case, I think you need to go to Project>Properties (in the menu at top)

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    15
    ok thanks!
    Dude, your getting a dell!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 02:30 AM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM