-3

我程序中的其他所有内容都运行正常,我只需要帮助设置一个 void 函数来计算最高付费客户。

/* Hector Gutierrez
     Date: November 27,2013
     Math 1900
     This program reads information from a file and displays it to an output file alone with there pay rate for the number of days they work
     */
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <cstdlib>
    # include<iomanip>
    using namespace std;
    struct Client_Bill_Info
    {
        string name;
        string adress1;
        string adress2;
        int numjobs;
        double hours;
        double min;
        double totaltime;
        double totalmin;
        double total;
        double totalhours;
        double paycheck;
    };
    void name(ofstream&);
    void getandprintadress ( ifstream& ,ofstream&,int,double);
    void openfiles( ifstream& , ofstream&, string,string);
    void highlypaid(ifstream&,ofstream&,int,double );
    int main()
    {

    ifstream getdata;
    ofstream outdata;

    int jobs;

    double payrate;
    string inputfile;
    string outputfile;

    openfiles( getdata,outdata,inputfile,outputfile);
    name (outdata);
    cout<<"How many Jobs "<<endl;
    cin>>jobs;
    cout<<"How much does the company charge per Hour: "<<endl;
    cin>>payrate;// how much you pay
    Client_Bill_Info customers;
    getandprintadress (getdata,outdata,jobs,payrate);
 return 0;
    }

我不确定我是否在主程序中正确调用了我的 void 函数

这是我试图创建的用于输出最高付费客户的 void 函数。我知道我必须创建一个if声明,但我不知道如何设置它。

void highlypaid (ifstream getdata,ofstream outdata,int jobs,double payrate)
{


}

这应该是一个 void 函数,它从一个文件中读取数据并将其输出到另一个文件,输出名称、地址、工作数量、工作时间和收入。

void getandprintadress (ifstream& getdata, ofstream& outdata, int jobs,double payrate)
{

    for (int m =0; m<jobs; m++)// By having cin>>employee here it allow for it to loop as many as number of employess you have
    {
        Client_Bill_Info customers;//this allows the structure to be included in the function
        customers.totalhours = 0;// All these variables must be set equal to zero in order to repeat sum of the valuse by reseting it back to zero
        customers.totalmin = 0;
        customers.totaltime= 0;
        //initially set employee.totalhours,totalmin,totaltime to 0
        getline(getdata, customers.name);// gets the first line of data which is the name
        getline(getdata, customers.adress1);// Second line of data that reads the adresss
        getline(getdata, customers.adress2);// third line of datat that reads the second part of adress
        //get the Employee's days worked.
        getdata>> customers.numjobs;
        outdata << "Customer Information "<<"\n";
        outdata << customers.name << "\n";
        outdata << customers.adress1 << "\n";
        outdata << customers.adress2 << "\n";
        outdata << "Number of jobs: " << customers.numjobs << "\n";
        for(int i=0; i<customers.numjobs;i++)// loops the program how many days work then reads the number of minutes worked below.
        {
            getdata>>customers.hours>>customers.min;//gets the hours and minutes displayed like this because it has to read the hours and minute in a single line
            customers.min = customers.min/60;//converts minutes to hours for instance 30 min is .5 hours
            customers.totalmin += customers.min;// after being converted from min to hours it takes the sum of minutes
            customers.totalhours += customers.hours;// taking the sum of the hours
            customers.totaltime = customers.totalmin + customers.totalhours;//adds the total minutes and time to calculate wage
            outdata<<"Job: "<< i+1 <<": "<<customers.hours<<" Hours "<<" and "<<customers.min*60<<" "<<" minutes "<<endl;
            string dummy;
            getline(getdata,dummy);

        }//calculate the employee's pay check amount
        customers.paycheck = (customers.totaltime * payrate);
        // outputs data to my outputfile
        outdata << fixed << showpoint << setprecision(2);
        outdata << "Amount of Bill: $ " << customers.paycheck << "\n\n";
        //Display the employee data to output file.
        getdata.ignore();//get the new line
        getdata.ignore();
    }
    return;
}


void openfiles( ifstream& getdata , ofstream& outdata, string inputfile,string outputfile )
{
    cout<<"what is the name of your inputfile"<<endl;
    cin>>inputfile;
    getdata.open(inputfile.c_str());
    if (getdata.fail())//test file as true
    {
        cout<<"Opening File Failed "<<endl;
        exit(1);
    }

    cout<<" What is the name of your output file"<<endl;
    cin>>outputfile;
    outdata .open(outputfile.c_str());
    if (outdata.fail())//test file if true
    {
        cout<<" File has Fail"<<endl;
        exit(1);
    }
    return;
}
void name(ofstream& outdata)
{
    outdata<<"Hector Gutierrez"<<endl;
    outdata<<"This program Will Read data from a file and output it to another."<<endl;
    outdata<<"Also this program specifically calculates the number of jobs one has work and"<<endl;
    outdata<<"how many total hours the company has work at the customers lawn."<<endl;
    outdata<<"This program will also calculate the highest payed customer he/she per week"<<endl;
    outdata<<endl;
    outdata<<endl;
    outdata<<endl;
    return;
}

这是我的输出文件

 Customer Information 
 Gavin K. Smith
 3928 Ottis Street
 Cleveland TN 37311
 Number of jobs: 5
 Job: 1: 1 Hours  and 34  minutes 
 Job: 2: 2 Hours  and 45  minutes 
 Job: 3: 1 Hours  and 55  minutes 
 Job: 4: 2 Hours  and 45  minutes 
 Job: 5: 0 Hours  and 30  minutes 
 Amount of Bill: $ 85.35

  ... repeat for other customers.... 

这就是我试图输出的内容:

支付最高的客户是 Name 每周支付的金额是 Amount

4

1 回答 1

2
  1. 声明一个变量。

  2. 将其初始化为第一个值。

  3. 循环遍历所有值(如果您愿意,可以跳过第一个值)。如果该值大于变量中的值,请将变量设置为该值。

  4. 您现在拥有该变量中的最高值,并且可以随心所欲地使用它。

于 2013-12-06T00:14:58.533 回答