14 Replies - 850595 Views - Last Post: 07 June 2009 - 05:29 PM Rate Topic: -----

#1 seanss916  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 23-May 06

sleep command in c++

Posted 23 May 2006 - 11:17 AM

how do you use the sleep command
Is This A Good Question/Topic? 1
  • +

Replies To: sleep command in c++

#2 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: sleep command in c++

Posted 23 May 2006 - 11:46 AM

well sleep function is used to suspend the execution of a program for certain time.u need to include dos.h header file to use it.the value pass to the function is taken in seconds
eg
cout<<"hello";
sleep(5);
cout<<"world";



for this code world will be printed 5 seconds after the printing of word hello.


there are many questions about time dealy functions in the previous threads of the forum.maybe they'll help u a bit :)
Was This Post Helpful? 0
  • +
  • -

#11 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: sleep command in c++

Posted 23 May 2006 - 11:50 AM

Syntax:

void sleep(unsigned seconds);

sleep() suspends execution for an interval (seconds).
With a call to sleep, the current program is suspended from execution for the number of seconds specified by the argument seconds. The interval is accurate only to the nearest hundredth of a second or to the accuracy of the operating system clock, whichever is less accurate.

This example should make it clear:
#include <dos.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   printf("Message 1\n");
   sleep(2); //Parameter in sleep is in seconds
   printf("Message 2 a two seconds after Message 1");
   getch();
   return 0;
}



sleep() is also compatible on *nix Systems.
Was This Post Helpful? 2

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: sleep command in c++

Posted 24 May 2006 - 05:22 AM

View Postborn2c0de, on 23 May, 2006 - 01:42 PM, said:

sleep() is also compatible on *nix Systems.

Not with those headers, I'm afraid...*nix systems have sleep declared in the unistd.h library.
Was This Post Helpful? 1

#13 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: sleep command in c++

Posted 24 May 2006 - 09:29 AM

Yes...I forgot about the function being defined elsewhere.
Thanks for mentioning it.
Was This Post Helpful? 0
  • +
  • -

#19 msg555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 04-May 06

Re: sleep command in c++

Posted 25 May 2006 - 06:15 PM

Just thought I'd add that "Sleep" is part of WinAPI

Syntax

VOID WINAPI Sleep(
DWORD dwMilliseconds
);

Sleep suspends the calling thread for the amount of milliseconds given by dwMilliseconds

http://msdn.microsof.../base/sleep.asp
Was This Post Helpful? 1

#20 ajaymatrix  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: sleep command in c++

Posted 01 September 2007 - 09:13 AM

nice information...
Was This Post Helpful? 0
  • +
  • -

#21 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: sleep command in c++

Posted 02 September 2007 - 01:24 AM

Keep in mind that the sleep() and the Sleep() API Function are not thread safe.
Was This Post Helpful? 1

#22 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: sleep command in c++

Posted 15 December 2007 - 04:10 PM

Sup guys, i am trying to do this but when i used born2code's code this happens:
8 C:\Documents and Settings\D3174\My Documents\Untitled1.cpp `sleep' undeclared (first use this function)

?
devc++ thanks
Was This Post Helpful? 0
  • +
  • -

#23 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: sleep command in c++

Posted 15 December 2007 - 05:38 PM

That's because 'sleep' is not a standard C or C++ function. You need to work out whether your compiler comes with an implementation of the sleep function (Dev-C++ usually uses MinGW. it might be in the "windows.h" header)

This post has been edited by Bench: 15 December 2007 - 05:39 PM

Was This Post Helpful? 1

#24 gcranston  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 31-January 08

Re: sleep command in c++

Posted 31 January 2008 - 02:47 PM

The Sleep(time in ms) function does require the header:

#include "windows.h"
Was This Post Helpful? 0
  • +
  • -

#25 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: sleep command in c++

Posted 31 January 2008 - 05:29 PM

Ty
Was This Post Helpful? 0
  • +
  • -

#26 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Re: sleep command in c++

Posted 17 April 2009 - 06:17 AM

The sleep command is not working at all I tried dos.h and windows.h and still doesn't want to work.
Was This Post Helpful? 0
  • +
  • -

#51 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: sleep command in c++

Posted 28 April 2009 - 07:51 AM

Oh This might help you sleep your program..

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
cout << "1st Message..."<<endl;
Sleep(1000); //Message Sleep for 1 Sec 1000ms = 1sec
cout << "2nd Message...";
getchar();
return 0;
}



Oh This might help you sleep your program..

TAKE NOTE USE CAPITAL "S" for sleep

ok thats great I hope i help im just noob

This post has been edited by nerodamsel: 28 April 2009 - 07:54 AM

Was This Post Helpful? 2

#52 joey325  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 07-June 09

Re: sleep command in c++

Posted 07 June 2009 - 05:29 PM

I made an easy example for all you noobs jk lol.

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int i;
    for(i=0; i<1000+1; i+=1)
    {
    system("CLS");
    cout << i << endl;
    Sleep(1000);
    }
    return 0;
}

This post has been edited by JackOfAllTrades: 07 June 2010 - 05:45 PM

Was This Post Helpful? 2

Page 1 of 1