Char by char output using sleep;

It seems to be a trivial problem but I can't figure it out.
I'm trying to output a string character by character using the sleep function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <iostream>
#include <cstring>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main()
{

        char output[] = "Hello there world";
        int len = strlen(output);

        for (int i = 0; i < len; i++)

        {

                cout << output[i];
                system("sleep 1");

        }

return 0;
}



It sleeps the amount of time it would have taken to print out the whole line and then it prints out the whole sentence at once. I've tried the sleep() and usleep() as well, but with the same result.
This also works fine on windows but not in Linux.
Any ideas?



Use std::flush after writing each character. You are seeing the effects of buffering.
Last edited on
Also, please don't use system() to sleep.
http://www.cplusplus.com/forum/unices/10491/#msg49054

Hope this helps.
Topic archived. No new replies allowed.