请指出具体在哪里以及需要进行哪些新的特定编辑。我总是遇到相同的错误,也不知道哪里出了问题。我已经折断了一百万人次了,很确定我做对了:
代码:
preg_match()
答案 0 :(得分:0)
您必须将函数放在main之前
void instructions()
{
cout << "This payroll program calculates an individual employee pay and";
cout << "\ncompany totals using data from a data file payroll.txt.\n";
cout << "\n\nA payroll report showing payroll information ";
cout << " is displayed.\n\n";
}
顺便说一句,为了保持一致性并提高可读性,您应该将所有行更改都放在开头或结尾处。否则,很难看到,例如,payroll.txt
和A payroll report
之间有3行...
// Other functions here…
// If some functions are dependant on others, those need to be declared before they are used.
int main()
{
// Some code here…
// Call your function
instructions();
// More code afterwards…
return 0;
}
或者,您只能像这样在main之前声明函数:
void instructions();
void reportTitle();
double calculateGross(double hours, double hourly);
double calculateNet(double grosspay, double netpercent);
// For documentation purpose, you should name your arguments.
// Also the body of your function does not appears to do what its name suggest.
void displayEmployeeInfo(const string &, double, double, double, double, double);
// Show probably named displayTotalAmounts
void totalAmounts(double tgross, double tnet);
您需要知道,在调用函数时必须传递适当的参数。例如:
int main() // partial implementation
{
double tgross = 1.0; // Whatever code you need to have desired value...
double tnet = 0.90;
totalAmounts(tgross, tnet);
return 0;
}
如果使用该选项,则可以在此处(在main之后)定义其他功能。
这给出了如何构造程序的基本思想。
阅读所有其他注释以查找代码中的其他问题!
还有一些其他事情:
fileName
,甚至对其进行了初始化,但随后您使用字符串打开了文件。double &tnet
。using namespace std
。net
似乎从未被初始化。if(!fin.is_open())
似乎可疑。假定没有错误,此时文件将打开,但在这种情况下,您可能要显示标题!string
初始化= ""
是没有用的,因为字符串有一个默认构造函数将其创建为空。if
或while
之类的关键字和左括号之间添加一个空格。totalAmounts
之后是否有空格,而其他功能却不是。fileName
小写时,为什么要为taxrate
使用驼峰大小写。如果使用小写字母,则应使用_分隔单词(例如tax_rate
),因为这样更易于阅读。total_net
(或totalNet
)比tnet
更容易理解。答案 1 :(得分:0)
此代码给出的警告少了一些,但您实际上必须自己整理代码。我也不想说,全局声明所有变量是一个很好的解决方案。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
string item = "";
ifstream fin;
double tgross = 0;
double tnet = 0;
double hourly;
double hours;
double taxrate;
double net;
void instructions()
{
cout << "This payroll program calculates an individual employee pay and";
cout << "\ncompany totals using data from a data file payroll.txt.\n";
cout << "\n\nA payroll report showing payroll information ";
cout << " is displayed.\n\n";
}
void reportTitle()
{
cout << setprecision(2) << fixed << showpoint << left
<< setw(20) << "Employee" << setw(10) << "Hourly" << setw(10) << "Hours"
<< setw(10) << "Tax" << setw(10) << "Gross" << setw(10) << "Net" << endl;
cout << setw(20) << "Name" << setw(10) << "Rate" << setw(10) << "Worked"
<< setw(10) << "Rate" << setw(10) << "Amount" << setw(10) << "Amount" << endl;
}
double calculateNet(double grosspay, double netpercent)
{
return grosspay - grosspay*netpercent/100.0;
}
void displayEmployeeInfo(const string &, double, double, double, double, double)
{
tgross += grosspay;
tnet += net;
}
void totalAmounts (double tgross, double tnet)
{
cout << "Totals" << setprecision(2) << fixed << showpoint << right
<< setw(50) << tgross << setw(10) << tnet << endl;
}
int main()
{
string fileName = "payroll.txt";
fin.open("payroll.txt");
if(!fin.is_open())
{
instructions();
reportTitle();
}
while(!fin.eof())
{
getline(fin,item,'#');
fin >> hourly >> hours >> taxrate;
double calculateGross(double hours, double hourly);
}
fin.close();
}