双重功能,fstreams,更有趣

时间:2013-12-07 16:30:55

标签: c++

重要的是将信息从一个文件传输到另一个文件。我做到了,效果很好。我需要做的下一件事是从新文件中找到最高值。基本格式是这样的:我有一堆“工人”,我有工作的日子,工作时间和工作时间。分钟他们工作了。在新文件中,我将其格式化为显示支付率(我输入值作为cin)然后我有他们所赚的总金额(每个人都使用相同的数量......在cin中)。出于某种原因,我无法创造一个能够提取赚钱最多的人名的功能(有效),以及多少钱。我有点沮丧,并尽我所能,所以新的功能是草率和荒谬,但我希望你能帮助。为了保持这个一般性的问题,而不是一个特定的问题,我想知道你们是否可以解释如何做这样的事情(找到最高价值和输出它与人的名字..所以基本上是一个双倍的钱和带有人名的字符串)以我的代码部分为例:

  #include <iostream>

  #include <fstream>

  #include <iomanip>

  #include <string>

  #include <cstdlib>

  using namespace std;

  void getandprintaddress (ifstream&, ofstream&); // this function will be used to get the data from one file and input it into the next with the total amount of money made from each person as well as the next function
  double highestpaid(double& highesttotal, double& totalpay, string highestpaidemp, string name); // this function is what will be used to find the highest amount of money and the person who has it
  void name(); // This is to input my name in the file.

  struct employees // This is the structure with all of the information that will be used to calculate everything. 
  {
     string name; // person's name
     string highestpaidemp; // highest paid employee
     string address; // the address of the employee
     string address2;
     int days; // days worked
     int hours; //hours worked
     double minutes; // minutes worked
      int total_hours; // total hours worked
     double totalpay; // total pay
     double payrate; // pay rate
     double total_minutes; // minutes worked
     double total_time; // total hours and minutes worked
     double highesttotal; // the highest total amount of money made between all employees.

};

int main(){
    ifstream inputFile; 
    ofstream outputFile;
    getandprintaddress(inputFile, outputFile);



return 0;
}

void getandprintaddress(ifstream& inputFile, ofstream& outputFile) // the main function that will be used to get and output all the information.
    {
        struct employees();
        ifstream getdata;
        ofstream outdata;
        int employees_t;

        employees employeeinfo;

        string inputfile;
        string outputfile;

cout << "What is the name of your input file? "<<endl; // this will be the file you open
            cin>>inputfile; 


    getdata.open(inputfile.c_str());


    if(getdata.fail()){ // this is meant to be used if someone enters a file that isn't there
            cout << "The input file has failed to open. \n";
            exit(1);
    }


    cout << "What is the name of your output file? \n"; // what you want the title of the new file to be.
                cin>>outputfile;
                outdata.open(outputfile.c_str());


if(outdata.fail())
{
   //This is if the new file is invalid

    cout << "The outputfile failed to open. \n";
    exit(1);


}


    cout << "How many employees are there? \n" ;
                    cin >> employees_t; // Enter how many employees there are

    cout << "What is the employees hourly payrate? \n";
                    cin >> employeeinfo.payrate; // how much each employee makes.

    for ( int info = 0; info < employees_t; info++)
         {
            employeeinfo.highesttotal = 0.0; // this will be needed for calculating the highest paid employee

            employeeinfo.total_minutes = 0.0; // This is needed to calculate total minutes

            employeeinfo.total_hours = 0; // Same as the total_minutes, but for hours instead.

            employeeinfo.total_time = 0.0; // Needed to calculate total time 

            string line1;

            getline(getdata, employeeinfo.name);

            outdata << "Name: " << employeeinfo.name <<endl; // employees name

            getline(getdata, employeeinfo.address);

            outdata << "Address: \n"; // Employees address

            outdata<< employeeinfo.address <<endl;

            getline(getdata, employeeinfo.address2);

            outdata <<employeeinfo.address2 <<endl;

            getdata >> employeeinfo.days;

            outdata << "Days worked: " <<employeeinfo.days << endl; // Days worked

            for (int totalworked=0; totalworked<employeeinfo.days; totalworked++)
                { 
                    // Because the employees work different amount of days, this loop is needed to post the individual information from each employee

                    getdata >> employeeinfo.hours >> employeeinfo.minutes;

                    employeeinfo.minutes = employeeinfo.minutes / 60;

                    employeeinfo.total_hours = employeeinfo.total_hours + employeeinfo.hours;

                    employeeinfo.total_minutes = employeeinfo.total_minutes + employeeinfo.minutes;

                    employeeinfo.total_time = employeeinfo.total_minutes + employeeinfo.total_hours;

                    employeeinfo.totalpay = employeeinfo.total_time * employeeinfo.payrate;

                    outdata << employeeinfo.hours <<" hours "<< employeeinfo.minutes*60 << " minutes"<< endl;

                }
            outdata << fixed << showpoint << setprecision(1); // Setting the total hours worked to 1 decimal so that to include the minutes

            outdata << "Total hours worked: "<<employeeinfo.total_time<<endl; // Total hours worked

            outdata << fixed << showpoint << setprecision(2); // Setting the decimal to two places

            outdata << "Hourly pay: $"<<employeeinfo.payrate << endl; //Hourly pay

            outdata << "Gross pay: $" <<employeeinfo.totalpay <<endl; // Gross pay

            getline(getdata,line1);

            getline(getdata,line1);

            outdata << "\n";

            double highestpaid(employeeinfo.highesttotal, employeeinfo.totalpay);
        }

    };
double highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name)
    {

        if (highesttotal < totalpay)
        {
            highesttotal = totalpay;
            highestpaidemp = name;


        }

return highestpaid(double& highesttotal, double&totalpay, string highestpaidemp, string name);
    };

1 个答案:

答案 0 :(得分:0)

好的,我会回答你的一般性问题,而不是花太多时间搜索你的代码,但希望这两者都适用。

您有结构员工

将他们全部放在员工的矢量中(我称之为工人)

vector<struct employee> workers

使用向量意味着您可以继续添加更多内容,但如果您拥有固定数量的工作人员,数组也会起作用

现在每个工人都可以通过整数索引0到n-1来识别,其中n是工人的数量。现在可以迭代(更快的方法,如果非常大的工人,但可能在这里很好),以找到最高薪水

double highestPay = 0; 
int highestPaidIndex = 0;
for(int i = 0; i++; i < n)
{
    if(workers[i].totalpay > highestPay)
    {
        highestPay = workers[i].totalpay;
        highestPaidIndex = i;
    }
}

现在,highestPaidIndex是收入最高的员工的索引,其名称为工人[highestPaidIndex] .name和总收入工人[highestPaidIndex] .totalpay

希望能解决您的一般问题和具体问题