c++ - 如何在输出中添加额外的零,同时仍使其与表格对齐?

标签 c++ c++11

   cout << setw(16) << "$" << setw(9) << fixed << showpoint 
               << setprecision(2) << Charge << setw(10) << KWHused 
               << setw(10) << setfill('0') << meter_after << setw(10) << setfill('0') << meter_before
               << setw(21) << Cust_Name;     

输出:

               ===================
               UTILITY BILLING LOG
               SPRING 2017
               ===================



                                       METER READINGS
                  CHARGE   KWH USED   CURRENT  PREVIOUS  CUSTOMER NAME
               ==========  ========  ========  ========  ===================
               $   163.00       70000043051500004304450000000 Edward L Jones
000000000000000$000240.75000000115000012365810001235431000000 Meryl Petersen

输入:

                         04304450  04305150 Edward L Jones
                         01235431  01236581 Meryl Petersen
                         00033333  00032223 Absent Dweller

预期输出:

                  ===================
                  UTILITY BILLING LOG
                  SPRING 2017
                  ===================


                                       METER READINGS                
                  CHARGE   KWH USED   CURRENT  PREVIOUS  CUSTOMER NAME
               ==========  ========  ========  ========  =================== 
               $   163.00       700  04305150  04304450  Edward L Jones
               $   260.30      1150  01236581  01235431  Meryl Peterson

其外观的链接:

http://www.cis.famu.edu/~cop3014cjoe/Homeworks/spring2017_PROG8_UtilityBilling

最佳答案

字段长度错误,只需添加所需的空格并重置填充字符即可:

cout << setfill(' ')
     << setw(16) << "$" << setw(9) << fixed << showpoint << setprecision(2) << Charge
     << "  "
     << setw(8) << KWHused 
     << "  "
     << setw(8) << setfill('0') << meter_after
     << "  "
     << setw(8) << setfill('0') << meter_before
     << "  "
     << setw(21) << Cust_Name
     << endl; 

关于c++ - 如何在输出中添加额外的零,同时仍使其与表格对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634168/

相关文章:

c++ - 不解析嵌套类实例或函数的类函数

C++/CPP 读取添加的十六进制地址

c++ - 不能将 typeof(std::endl) 作为模板参数?

c++ - gluPerspective在什么情况下会看到顶点?

c++ - 将 istream 参数传递给类构造函数时出现问题

c++ - 错误 : LNK 2019 : unresolved external symbol _imp_CrtDbgReportw in Visual Studio

c++ - 未正确推断对全局函数的引用

C++11 元编程 : check for method existence

C++ 链表 - 构造函数和运算符重载

c++ - 跳过 QDomDocument 中的注释