如何在运行控制台时更改文本

编程入门 行业动态 更新时间:2024-10-22 21:19:10
本文介绍了如何在运行控制台时更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复:

我的问题是,如何在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'表示回车(转到行首), 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.

更多推荐

如何在运行控制台时更改文本

本文发布于:2023-11-15 16:57:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   台时   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!