提问逐个输出

ssbelle 2002-10-15 12:07:18
这个程序是想让字母一个一个的显示在屏幕上,有一定的时间间隔,f()函数的间隔大概
是半秒,但是如果是没有下面的cout<<endl ,则大概等四五秒以后将what is your name
一并输出,加上cout<<endl 以后则每半秒输出一个字母并换行。不知道为什么。


还有,要是想让汉字也一个一个的输出应该怎么办呢?
#include "stdafx.h"
#include <iostream.h>
void f()
{
long double s=0;
for(long double i=0;i<10000000;i++)
s+=i;
}

main()
{
const char* str="what is your name?";
while(*str)
{
f();
cout<<*str++;
//cout<<endl;

}
int a;
cin>>a;
}

...全文
30 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssbelle 2002-10-18
  • 打赏
  • 举报
回复
用putchar(*str++);的确也可以的.
ssbelle 2002-10-18
  • 打赏
  • 举报
回复
谢谢各位,问题解决了!
hannibalhontani 2002-10-17
  • 打赏
  • 举报
回复
用putchar(*str++);
就行了。
ssbelle 2002-10-17
  • 打赏
  • 举报
回复
请教一下Sleep函数的具体用法?
另system("pause");的作用?
ssbelle 2002-10-15
  • 打赏
  • 举报
回复
to 百宝箱的看法:
那应该怎样实现呢?
还有汉字的输出
anrxhzh 2002-10-15
  • 打赏
  • 举报
回复
这是因为标准输入输出流是带有缓冲的,当输出流遇到std::endl时会强制地刷新缓冲,所以就不会出现停滞了。
GOTO_2002 2002-10-15
  • 打赏
  • 举报
回复
啊,打错了,是\0
sorry~~~
GOTO_2002 2002-10-15
  • 打赏
  • 举报
回复
cout <<这是个运算符,它以/0结尾标志,你定义的what is your name 中 w 后面没有/0,h后面也没有/0,自然不会按照你最初的想法输出了。
muche 2002-10-15
  • 打赏
  • 举报
回复
因为str是个字符指针,输出一个*str内容后,str指向一个内容,所以,当cout<<endl就出现了你所说的情况了!
Tipel 2002-10-15
  • 打赏
  • 举报
回复
我在tc++3中编译运行,没有问题
不需要cout<<endl就可以的
输出汉字,这样可不可以?
f()
count<<*str++
count<<*str++
huangxs 2002-10-15
  • 打赏
  • 举报
回复
不用加COUT<<ENDL可以的我试过
anrxhzh 2002-10-15
  • 打赏
  • 举报
回复
将汉字当作两个字符输出,结果跟平台有关系,不利于移植。
标准的方法应该是将汉字当作单个字符输出,这样就要看你用的是什么字符集了。
benbenli 2002-10-15
  • 打赏
  • 举报
回复
anrxhzh(百宝箱) pasted out an excellent answer when I was writing my answer.

There is a typing mistake in my answer: "in the same name" should be "in the same line".
benbenli 2002-10-15
  • 打赏
  • 举报
回复
If you want to output all characters in the same name with delays between characters, you have to flush manually after you output every character. You may change this line "cout<<*str++;" to "cout<<*str++ << flush;" or "cout<<*str++; cout.flush();".
If you need to output Chinese characters, you need to output 2 bytes once because one Chinese character is represented by 2 bytes.

If you want to output ASCII characters mixed with Chinese characters, you have to check the byte before you output it. If the byte is less than 128, it is an ASCII character. So you simply output it and delay. If it is greater than or equal to 128, it is the first byte of a chinese character and the subsequent byte is the second byte. You need to output the 2 bytes and delay.

For the delay function f(), you are using loop. It's hard to control the time because this is related to the CPU speed. You may use function sleep(milliscond) in VC++. If you are working on UNIX platform, you may use system call usleep(microseconds).
anrxhzh 2002-10-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdlib>
#include <windows.h>

int main()
{
const char s[]="hello world 世界你好";
for(int i=0;i<sizeof(s);++i){
std::cout << s[i] << flush;
Sleep(500);
}
system("pause");
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧