usleep

Why do std::cout and printf() not print between usleep() delays?


i have a program that is supposed to print "Hello, World!" in slowly scrolling text. I am using the unistd.h library for the usleep() function, and i'm using std::cout to print the characters to the standard output:

    #include <iostream> 
    #include <stdio.h>   
    #include <unistd.h>
    char hello[13]={'H','e','l','l','o',',',' ','W','o','r','l','d'};

    int main (){
         for(int i=0; i<14; i++){
              std::cout<<hello[i]; //it prints the entire string after
              usleep(100000);      //100000 ms, but it should print a char after
              }                    //every 100 ms.
    }

Solution

  • You might need to flush the output stream.