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

非 Y 不嫁゛ 提交于 2019-12-04 06:41:56

问题


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.
    }

回答1:


You might need to flush the output stream.



来源:https://stackoverflow.com/questions/30240251/why-do-stdcout-and-printf-not-print-between-usleep-delays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!