循环中的Cout不会按定义按字符打印char [英] Cout in loop doesn't print char by char as defined
问题描述
我最近开始在大学学习C ++,并决定在家里进步一点。我有个想法,就是编写一个程序,给定一段文本,然后逐个字符地打印出这样的文本(在这段视频中 SUPER.HOT聊天)。
I've recently started learning C++ at university and decided to advance a bit at home. I had the idea of making a program that, given a piece of text, would print out such text character by character with a small delay in-between (as seen in this video SUPER.HOT chat).
我尝试使用一个简单的过程来重新创建它:
I tried to recreate it using a simple procedure:
void typer(string text){
for (int i = 0; i < text.length(); i++){
cout << text[i];
usleep(100000);
}
但是当usleep()设置为103900以下时,它将开始打印一次两个字符。我的意图是一次只打印1张,但是很快。
But when usleep() is set under 103900, it'll start printing out two characters at a time. My intention is to print only 1 at a time but very quickly.
有什么建议吗? :D
Any suggestions? :D
推荐答案
您需要刷新流,否则它将被缓存
You need to flush the stream, otherwise it will be cached
cout.flush();
http://www.cplusplus.com/reference/ostream/basic_ostream/flush/
这篇关于循环中的Cout不会按定义按字符打印char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!