【问题标题】:How to use setw and setfill together in C++ to pad with blanks AND chars?如何在 C++ 中一起使用 setw 和 setfill 来填充空格和字符?
【发布时间】:2013-09-23 14:45:07
【问题描述】:

我需要为家庭作业编写一个程序来计算学费,并且输出的格式应该像这样,在美元符号后使用点填充和空格填充:

Student Name:                Name goes here
Address:                     Address goes here


Number of credits: ..........         5
Cost per credit hour: ............ $ 50

到目前为止我的代码是:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

double const REG_FEE = 700, STUDENT_ASSEMBLY_FEE = 7.19, LEGAL_FEE = 8.50, STUDGOV_FEE = 1.50, 
LATE_FEE_PERCENTAGE = 0.015; 



int main()

{ double num_credits, cost_per_credit, tuition_total, late_charge, amount_due; 

string student_name;
string student_address; 
string student_city_state_ZIP;


ifstream info;
info.open ("info.txt");


getline (info, student_name);
getline (info, student_address);
getline (info, student_city_state_ZIP);
info >> num_credits;
info >> cost_per_credit;

tuition_total = num_credits * cost_per_credit + REG_FEE + STUDENT_ASSEMBLY_FEE + LEGAL_FEE
+ STUDGOV_FEE;
late_charge = tuition_total * LATE_FEE_PERCENTAGE;
amount_due = tuition_total + late_charge;


cout << "Tuition and Billing Program by Neal P." << endl
<< setw(18) << "Student Name:" << student_name << endl
<< setw(18) << "Address:" << student_address << endl
<< left << setfill('.') << endl
<< setfill(18) << "Number of Credits:" << setw(5) << "$" <<  num_credits << endl
<< setfill(18) << "Cost per Credit Hour:" << setw(5) << "$" << cost_per_credit << endl
<< setfill(18) << "Tuition Cost:" << setw(5) << "$" << tuition_total << endl
<< setfill(18) << "Registration Fee:" << setw(5) << "$" << REG_FEE << endl
<< setfill(18) << "MSA Fee:" << setw(5) << "$" << STUDENT_ASSEMBLY_FEE << endl
<< setfill(18) << "Legal Services Fee:" << setw(5) << "$" << LEGAL_FEE << endl
<< setfill(18) << "Student Government Fee:" << setw(5) << "$" << STUDGOV_FEE << endl;

return 0; 

}

当我编译时,我得到一个很长的错误,比如:“在函数‘int main()’中: /Users/nealp/Desktop/machine 问题 2.cpp:41: 错误: no match for 'operator*)std::operator&)((std::basic_ostream >*)((std::basic_ostream >*)((std::basic_ostream >*)std::operator

【问题讨论】:

  • setfill(18) 在“学分数量”等之前看起来很奇怪......
  • 没有理由将输出全部放在一行中,您可以使用多行,例如cout &lt;&lt; "Tuition and Billing Program by Neal P." &lt;&lt; endl; cout &lt;&lt; setw(18) &lt;&lt; "Student Name:" &lt;&lt; student_name &lt;&lt; endl;" 使您的代码和错误消息更具可读性。

标签: c++ string


【解决方案1】:

std::setfill 是一个function template,它的模板参数是根据你传递给它的参数的类型推导出来的。它的返回类型是未指定的——它是一些实现定义的代理类型,它在流对象上设置填充字符并启用operator&lt;&lt; 调用的进一步链接。它还取决于 setfill 被实例化的类型。

文字18 的类型是int,所以setfill 返回一些不相关的类型,没有operator&lt;&lt; 可用。

我不确定你所说的setfill(18) 是什么意思,我猜你把它误认为setw(18)

【讨论】:

    【解决方案2】:

    只需替换代码中的setfill(18),这就是编译错误的原因。

    这是为了满足要求而改进的打印部分,

    cout << "Tuition and Billing Program by Neal P." << endl
    << "Student Name:" << student_name << endl
    << "Address:" << student_address << endl
    <<endl<<endl<< "Number of Credits:" << setfill('.') << setw(5) <<"$" <<  num_credits   
    << endl<< "Cost per Credit Hour:"<<setfill('.')<<setw(5)<< "$" << cost_per_credit << 
    endl<< "Tuition Cost:" << setfill('.')<<setw(5)<< "$" << tuition_total << endl
    << "Registration Fee:" << setfill('.')<<setw(5) << "$" << REG_FEE << endl
    << "MSA Fee:" << setfill('.')<<setw(5) << "$" << STUDENT_ASSEMBLY_FEE << endl
    << "Legal Services Fee:" << setfill('.')<<setw(5) << "$" << LEGAL_FEE << endl
    << "Student Government Fee:" << setfill('.')<<setw(5) << "$" << STUDGOV_FEE << endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多