0

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

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: error: no match for 'operator<<' in ' ((std::basic_ostream >*)std::operator<< [with _CharT = char, _Traits = std::char_traits]((((std::basic_ostream >&)((std::basic_ostream >*)((std ::basic_ostream >*)((std::basic_ostream >*)std::operator<<" 继续。这是我同时使用 setw 和 setfill 的问题吗?我知道 setfill 只需声明一次并且setw 只对下一行输出有效,但我每次都定义了 setfill,因为我在它之前使用了 setw。

4

2 回答 2

2

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

文字的类型18返回一些intsetfill相关的类型,没有operator<<可用的类型。

我不知道你的意思是什么setfill(18),我猜你把它弄错了setw(18)

于 2013-09-18T18:12:48.390 回答
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;
于 2013-09-18T18:12:37.870 回答