如何在C ++中一起使用setw和setfill来填充空格和字符?

时间:2013-09-18 17:53:28

标签: c++ string

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

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 /桌面/机器问题2.cpp:41:错误:'(&std :: basic_ostream&gt; *)std :: operator&lt;&lt; [with _CharT = char]中'运算符&lt;&lt;'不匹配,_Traits = std :: char_traits](((std :: basic_ostream&gt;&amp;)((std :: basic_ostream&gt; *)((std :: basic_ostream&gt; *)((std :: basic_ostream&gt; *) std :: operator&lt;&lt;“继续。 这是我同时使用setw和setfill的问题吗?我知道setfill只需要声明一次,setw只对下一行输出有效,但我每次都定义了setfill,因为我之前使用的是setw。

2 个答案:

答案 0 :(得分:2)

std::setfillfunction template,它的模板参数会根据您传递给它的参数的类型推断出来。它的返回类型是未指定的 - 它是一些实现定义的代理类型,它在流对象上设置填充字符,并允许进一步链接operator<<调用。它还取决于setfill实例化的类型。

文字18的类型为int,因此setfill会返回某些不相关的类型,但没有operator<<可用。

我不确定你对setfill(18)的意思是什么,我猜你错了setw(18)

答案 1 :(得分:0)

只需在代码中替换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;