如何在运行控制台时更改文本 [英] How to change text while running the console

查看:121
本文介绍了如何在运行控制台时更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我的问题是,如何在C ++中运行控制台窗口时更改文本。例如。

my question is, how do I change text while RUNNING a console window in C++. For example.

如果我要显示这个。

cout<<"0%";
cout<<"25%";
cout<<50%";
cout<<75%";
cout<<"100%";

它会产生5个不同的词。如果我想让它显示0%然后25%不想做一个新的话,I.E替换当前的0%用25%。这是可能吗?提前感谢。

It will make 5 different words. What if I want it to display 0% then 25% withought making a new word, I.E replacing the current 0% with a 25%. Is this even possible? Thanks in advance.

推荐答案

使用 cout<数字<< '\r'<< flush

'\r'表示回车(转到行首),< c $ c> flush 表示确保刚刚打印的输出到达输出。通常输出仅在提供行结束时打印。

The '\r' means "carriage return" (go to beginning of line", the flush means "make sure what I've just printed reaches the output now. Normally output is only printed when a end of line is provided.

编辑:如果你有一个输出长度变化的情况,例如倒计时,你将不得不用足够的空间填充输出以覆盖任何额外的输出,例如 cout< ;< setw(3)<< number ... cout<<<< >

If you have a situation where the length of the output varies, e.g. counting down, you will have to pad the output with sufficient spaces to cover any extra output. For example cout << setw(3) << number ... or cout << number << " " ... would work.

但是,请注意,如果你的行长度超过了终端/命令窗口的宽度,它可能会变得麻烦。

Be aware, however, if your line gets longer than the width of the termina/command windo, it may become messy.

这篇关于如何在运行控制台时更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆