Question

Everything else in my program is running correctly i just need help to set up a void function to calculate the highest paid customer.

/* 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;
    }

I'm not sure if I'm calling my void function correctly in my Main program

This is the void function that I'm trying to create to output the highest paid customer. I know I have to create an if statement but I don't know how to set that up.

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


}

This should be a void function that reads data from a file and outputs it to another, outputting name, address, number of jobs worked, hours they worked and how much they made.

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;
}

This is my output file

 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.... 

This is what i trying to output:

The highest paid customer is Name the amount paid per week is Amount

Was it helpful?

Solution

  1. Declare a variable.

  2. Initialize it to the first value.

  3. Loop through all the values (you can skip the first one if you like). If that value is greater than the value in the variable, set the variable to that value.

  4. You now have the highest value in that variable and can do whatever you like with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top