我的代码有问题,不确定是错误还是我的代码有问题。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setfill('*') << setw(80) << "*";
cout << setw(21) << "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
添加手动空格有效,但我想以编程方式添加空格,但是当我测试应用程序时,它看起来像这样:

setw 不移动文本但设置它应该采用的最小宽度
为了实现您的想法,您应该尝试使用更大的值,因为您的字符串长度超过 21 个字符,例如
cout << setfill('*') << setw(80) << "*" << endl;
cout << setfill(' ') << setw(56) << "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;
Run Code Online (Sandbox Code Playgroud)
输出:
********************************************************************************
Mt.Pleasant Official Billing Statement
********************************************************************************
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10511 次 |
| 最近记录: |