Search code examples
c++delaywaitclockcout

Why is this output waiting for something after it?


void Wait(double Duration)
{
    clock_t End;
    End = clock() + (Duration*CLOCKS_PER_SEC);

    while (clock() < End)
    {
        // This loop just stalls the program.
    }
}

My function works perfectly half the time, but it occasionally stalls the program before it's even called. For example, take the following snippet:

cout << "This is\n";
Wait(2.5)
cout << "a test!";

You'd expect the first line to appear immediately and the second line to appear after 2.5 seconds, but it sometimes ALL appears after 2.5 seconds. What's the deal?


Solution

  • try

    cout.flush();
    

    before your Wait