I asked before for a time delay function which is like system ("pause") but depend on time instead of keys , to elaborate i want the program instead if writing " press any key to contiue " i want it to freeze for example for 2 or 3 sec then continue. i got replies which i cant understand, its just codes without explaining the meaning of the function or the input or the output. plz reply me again with more explanation . i really need that facility and i want to understand it.
"NOTE: i am using simple console app with visiual C++ 6 compiler"
thnx
hope u can reply me soon
bye

Dani AI

Generated

Short answer: the reason both "Hello" and "World" showed up together is almost certainly output buffering, not the timer call itself. and were on the right track about different delay functions, and showed a clock-based loop, but the missing piece is that text sent to std::cout may be held in a buffer until a newline or an explicit flush occurs. For a visible "Hello..." immediately, flush the stream before waiting.

A simple, modern and portable pattern (C++11 and later) is to flush, then sleep without burning CPU:

#include <iostream>
#include <thread>
#include <chrono>

int main() {
    std::cout << "Hello..." << std::flush;                 // force output now
    std::this_thread::sleep_for(std::chrono::seconds(2));  // wait ~2 seconds
    std::cout << "World\n";
}

Notes and cautions:

  • On older compilers (VC++6) there is no std::this_thread; use the platform API available there (the Win32 Sleep function on Windows) but still call std::cout.flush() (or print std::endl) before sleeping so the first message appears immediately. already pointed to platform functions; remember different platforms use different names and time units (so check the docs for your compiler).
  • Avoid busy-wait loops like the while(clock()-start < delay) pattern unless you have a very specific need; they consume CPU and are usually less accurate. demonstrated the approach; it works but is inefficient.
  • Sleep-style calls block the current thread. For GUI apps or when the program must remain responsive, use timers or run the delay in a background thread instead.
  • Sleep durations are minimums; actual wake time depends on OS scheduler and timer resolution. For high-precision timing look to high-resolution timers on your platform.

These points solve the typical "everything appears after the wait" problem and give safer choices for both old and modern toolchains.

Recommended Answers

All 6 Replies

yea... it's very useful... here's the code:

#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;

int main() 
{
	 cout << "Hello...";
	 Sleep(2000);
	 cout << "World...";
	 cin.get();
 
	 return 0;
}//end main function

if you do not have the windows.h header for some odd reason unknown to me, which you have VC++6 so you should, try to run a loop a bunch of times through to give it that effect which windows.h basically does. it just measures your processor speed and does an equation using that... peace...

Hey not sleep(2000) bleek, sleep(4) might be a nice number if sleep() is the same for your complier as for mine.I use Borland C++ and sleep() counts in seconds.

There is also delay(int) funtion.It counts in some unit lower than a second.
#include <stdlib.h>
#include <dos.h>

(I dont know which compiler you guys use)
:surprised :confused:

yea... it's very useful... here's the code:

#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;

int main() 
{
	 cout << "Hello...";
	 Sleep(2000);
	 cout << "World...";
	 cin.get();
 
	 return 0;
}//end main function

if you do not have the windows.h header for some odd reason unknown to me, which you have VC++6 so you should, try to run a loop a bunch of times through to give it that effect which windows.h basically does. it just measures your processor speed and does an equation using that... peace...

thnx for ur reply
but can u explain more plz .
when i use this code is makes the whole word "hello world" appears after 5 sec apox. i want appear to appear then after 5 sec world continue.
plz explain what is 2000 .
and some1 told me that this function have bad effects on the pc "This is a reply u can see it if u searched after before that post by few ones"
so plz tell me that stuff
and really thnx
bye

it should work, it works perfectly fine for me... in my dev-cpp compiler it counts with thousands of a second or something so 2000 is 2000 thousandths which makes 2 seconds... *stops and takes a deap breath*

i dont know what you want me to explain further... peace

i have a good one from my book. :)


#include <iostream.h>
#include <time.h> // my compiler is out dated :) update as needed


int main()
{
cout << "enter delay time, in seconds : ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC ;
cout << "starting\a\n";
clock_t start = clock();
while (clock() - start < delay );
cout << "done \a\n";
return 0;

}

yea... i had to use that one time in school because i was programming in a MAC lab and they didnt have windows.h for me to use Sleep() with...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.