简体   繁体   English

setw()在我的代码上无法正常工作

[英]setw() not working properly on my code

I'm having problem with my code not sure if it's a bug or there is something wrong with my code. 我的代码有问题,不确定是否是错误或我的代码有问题。

#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;
}

Adding manual spaces works but I want to add spaces programmatically but when I tested the application this what it looks like : 添加手动空间有效,但我想以编程方式添加空格,但当我测试应用程序时,它看起来像:

在此输入图像描述

setw does not move the text but sets the minimum width it should take setw 不会移动文本,但会设置应该采用的最小宽度

To achieve what you have in mind you should experiment with a bigger value since your string is longer than 21 characters, eg 要实现您的想法,您应该尝试更大的值,因为您的字符串超过21个字符,例如

cout << setfill('*') << setw(80) << "*" << endl;
cout  << setfill(' ') << setw(56) <<  "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;

Output: 输出:

********************************************************************************
                  Mt.Pleasant Official Billing Statement
********************************************************************************

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM