-4

请指出具体的地方以及需要进行哪些新的具体编辑。我不断收到相同的错误,我不知道出了什么问题。我已经检查了 100 万次括号,并且很确定我做对了:

  • cpp:36: 错误: '{' 标记之前不允许函数定义
  • cpp:44: 错误: '{' 标记之前不允许函数定义
  • cpp:58: 错误:“double”之前的预期初始化程序</li>
  • cpp:63: 错误: '{' 标记之前不允许函数定义
  • cpp:69: 错误: '{' 标记之前不允许函数定义

代码:

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

int main()
{   
    string item = "";
    ifstream fin;
    double tgross = 0;
    double tnet = 0;
    double hourly;
    double hours;
    double taxrate;
    double net;

    string fileName = "payroll.txt";    
    fin.open("payroll.txt");

    if(!fin.is_open())
    {   
        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;
        }
    }

    while(!fin.eof())
    {
        getline(fin,item,'#');
        fin >> hourly >> hours >> taxrate;

        double calculateGross(double hours, double hourly)
        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;
    }

    fin.close();
}
4

2 回答 2

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.txtA 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我建议您在关键字 like orwhile和左括号之间添加一个空格。
  • 此外,您应该保持间距一致。虽然您在 while 之后是否有空间totalAmounts用于其他功能,但事实并非如此。
  • 并为您命名变量。为什么你使用骆驼大小写,fileName而whiletaxrate是小写的。如果您使用小写字母,那么您应该使用 _ 来分隔单词(例如tax_rate),因为这样更易于阅读。
  • 命名变量时应避免缩写。total_net(or totalNet) 比 . 更容易理解tnet
  • 通常当您遇到编译器错误时,第一个问题是在编译器报告的位置附近。修复该错误,然后检查其他错误是真正的错误还是第一个错误的结果。在这种情况下,它有助于编译单个文件(对于具有数百个文件的大型生产项目)。
于 2018-11-05T02:20:39.360 回答
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();
}
于 2018-11-05T02:21:59.593 回答