您需要爲每個強度點打印:
,並用空格(容量)填充其餘部分。有幾種方法。
方法1:迴路
cout << '{';
for (unsigned int i = 0; i < capacity; ++i)
{
if (i < strength)
{
cout << ':';
}
else
{
cout << ' ';
}
}
方法2:的字符串重複字符
const std::string strength_text(strength, ':');
const std::string filler_text(capacity-strength, ' ');
cout << '{';
cout << strength_text << filler_text;
cout << '}';
方法3:設置字段寬度
const std::string strength_text(strength, ':');
cout << '{';
cout << setw(capacity) << strength_text;
cout << '}';
您應該研究setw
修飾符以查看它是否執行右對齊或左對齊填充。
其他方法涉及光標定位,它需要一個外部庫。
你有什麼嘗試過,那麼它不起作用?考慮[編輯]你的問題,以包含[mcve] – Tas
一般來說,你需要嘗試做出一些事情。 –
如果您需要重新定位光標,則需要使用*光標定位*庫。您可以使用'\ r'將回車返回到行的開頭,但取決於控制檯(終端)仿真。 –