C++ 如何在C+中同时使用setw和setfill+;用空白和字符填充?

C++ 如何在C+中同时使用setw和setfill+;用空白和字符填充?,c++,string,C++,String,我需要为家庭作业编写一个程序来计算学费,输出的格式应该是这样的,在美元符号后加上点填充和空格填充: Student Name: Name goes here Address: Address goes here Number of credits: .......... 5 Cost per credit hour: ............ $ 50 到目前为止,我的代码是: #include <

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

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; 
#包括
#包括
#包括
#包括
使用名称空间std;
双倍注册费=700,学生大会费=7.19,法律费=8.50,学生管理费=1.50,
滞纳金百分比=0.015;
int main()
{双倍学分、每学分成本、学费总额、滞纳金、应付金额;
学生姓名字符串;
字符串学生地址;
字符串学生\城市\州\邮编;
ifstream信息;
info.open(“info.txt”);
getline(信息、学生姓名);
getline(信息、学生地址);
getline(信息、学生、城市、州、邮编);
信息>>学分数;
信息>>每个信用卡的成本;
学费总额=学分数*每学分成本+注册费+学生集会费+法律费
+学费;
滞纳金=学费总额*滞纳金百分比;
应付金额=学费总额+滞纳金;
cout只需替换代码中的
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;

cout
std::setfill
是一个模板参数,它的模板参数根据您传递给它的参数的类型进行推导。它的返回类型未指定-它是一些实现定义的代理类型,用于在流对象上设置填充字符,并启用之前的
操作符
setfill(18)
的进一步链接“学分数”等看起来很奇怪……没有理由将输出全部放在一行中,您可以使用多行,例如
cout