-
December 12th, 2013, 09:54 PM
#31
Re: need help my (employee payroll program using structures)does not show all data
Because what I'm doing in my writeReport function:
Code:
double totalgross, totgrosspay;
totgrosspay = (emp[i].hoursworked * emp[i].hourly rate);
totalgross = totgrosspay;
and what does this statement mean:
Make an array of structures in main (function) to allow you to do the math for each employee.
-
December 12th, 2013, 11:01 PM
#32
Re: need help my (employee payroll program using structures)does not show all data
-
December 13th, 2013, 06:44 AM
#33
Re: need help my (employee payroll program using structures)does not show all data
Originally Posted by Krrish1
Because what I'm doing in my writeReport function:
Code:
double totalgross, totgrosspay;
totgrosspay = (emp[i].hoursworked * emp[i].hourly rate);
totalgross = totgrosspay;
and what does this statement mean:
Make an array of structures in main (function) to allow you to do the math for each employee.
Who wrote the code in the OP? You say it's yours, but you don't seem to understand it. The array of structs is already there.
payrollStruct employee[SZ]; // array of golfers and data
-
December 13th, 2013, 06:45 AM
#34
Re: need help my (employee payroll program using structures)does not show all data
Originally Posted by Krrish1
Because what I'm doing in my writeReport function:
double totalgross, totgrosspay;
totgrosspay = (emp[i].hoursworked * emp[i].hourly rate);
totalgross = totgrosspay;
Gross pay is (regular hours worked * hourly pay) + (overtime hours worked * overtime pay)
-
December 13th, 2013, 06:55 AM
#35
Re: need help my (employee payroll program using structures)does not show all data
He's already showing grosspay per employee (post #19). What he wants is to sum the grosspay for each employee and output this sum value at the end of the report - hence my suggestion in post #20.
But from his post #31, he's got 2 problems. He's not initialising totalgross and he's not adding totalgrosspay to totalgross.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 13th, 2013, 03:08 PM
#36
Re: need help my (employee payroll program using structures)does not show all data
I don't know what to do, so confused:
An example would help what is wanted to get that result
-
December 13th, 2013, 03:30 PM
#37
Re: need help my (employee payroll program using structures)does not show all data
Originally Posted by Krrish1
I don't know what to do, so confused:
An example would help what is wanted to get that result
This is an example of obtaining a total
Code:
#include <iostream>
using namespace std;
const int no = 6;
double pay[no] = {12.34, 45.67, 12.56, 78.89, 23.57, 65.98};
int main()
{
double total = 0;
for (int i = 0; i < no; i++)
total += pay[i];
cout << "total is: " << total << endl;
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 13th, 2013, 11:18 PM
#38
Re: need help my (employee payroll program using structures)does not show all data
Here is my updated code ( where there is bold sentence that sentence has been updated)
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;
const int SZ = 20; // size of arrays to hold employee
struct payrollStruct // strucute of employees
{
double hoursworked; // employess hours worked
double hourlyrate; // employees rate of pay
string empID; // employee ID
string firstname; // first name of employee
string lastname; // last name of employee
double regularpay; // employee regular pay
double overtimepay; // employee overtime pay
double overtimehours; // employee overtime hours
double grosspay; // employee gross pay
int regularhours; // employee regular hours
int bubbleSort; // sort employees through last name
int swap; // swaps employees last name
double totalgrossPay;
};
void tellUser(); // tells the user about the employee what the program does
int readInput(payrollStruct []); // reads data from a txt file
void bubbleSort(payrollStruct [], int); // sorts the through employees last name
void swap(payrollStruct *, payrollStruct *); // swaps the lastname throught alphabertical order
void findregularhours(payrollStruct [], int); // calculates if the employee has exceeded it regular hours or not
void findregularpay(payrollStruct [], int); // calculates the employees regular pay
void findovertimehours(payrollStruct [], int); // calculates if the employee has exceeded it overtime hours or not
void findovertimepay(payrollStruct [], int); // calculates the employees overtime pay
void findgrosspay(payrollStruct [], int); // calculates the employees gross pay
int writeReport(payrollStruct [], int); // writes/shows employees payroll information on screen and prints to outputFile
void findtotalPay(payrollStruct []);
int main()
{
int i, code, ret; // counter
payrollStruct employees[SZ]; // array of golfers and data
int numemp; // total number of golfers
tellUser(); // tell user what program does
numemp= readInput(employees);
cout << "There were " << numemp << " golfers\n\n"; // how many employees are there
if (numemp < 1)
{
cout << "Program ending\n\n";
code = 1;
}
else
{
// sort employees
cout << "Sorting the employees in order with the lastname first\n";
bubbleSort(employees, numemp); // bubble sort employees lastname
findregularhours(employees, numemp); // calculates if the employee has exceeded it regular hours or not
findregularpay(employees, numemp); // calculates the employees regular pay
findovertimehours(employees, numemp); // calculates if the employee has exceeded it overtime hours or not
findovertimepay(employees, numemp); // calculates the employees overtime pay
findgrosspay(employees, numemp); // calculates the employees gross pay
findtotalPay(employees);
code = writeReport(employees , numemp); // writes/shows employees payroll information on screen and prints to outputFile
}
return code;
}
/* ***************************************************************
* function tellUser
* it tells the user what the program does
* it prints output to the screen
* input: none output: tells user on screen what program does
* returns: none
****************************************************** */
void tellUser()
{
cout << "\nThis program reads a file called employees.txt,\n";
cout << "and it calculates the regular, overtime and gross pay for each golfer.\n";
cout << "employees are sorted into order with employees last name.\n";
cout << "output is written to the screen. and to an output file\n";
cout << "It uses an array of structures to store the data\n";
}
/* *************************************************************************8
* readInput
* This function reads employees data from a file "employees2.txt"
* It stores the first and last names of employees regular, overtime, grosspay
* for each employee in an array of structures
* input: first and last names, regular, overtime, grosspay of each person
* passes back: array of employee data in structure
* returns: number of employees
******************************************************************************* */
int readInput(payrollStruct emp[])
{
ifstream inputFile; // input filename in program
int numEmp; // number employees
int i;// counter
// open file and read input from file employees2.txt
inputFile.open("employees2.txt");
if (inputFile.fail())
{
cout << "Error opening file employees2.txt\n\n"; //error opening txt file
cout << "end of program\n\n";
return 0;
}
else
{
i = 0; // employee number for array
while ( ( i < SZ) && (inputFile >> emp[i].hoursworked))
{ // there is a line of data to read and array has room
inputFile >> emp[i].hourlyrate;
inputFile >> emp[i].empID;
inputFile >> emp[i].firstname;
inputFile >> emp[i].lastname;
i++; // ready for the next employee
} // end while
numEmp = i; // number employee
inputFile.close(); // close the file after reading it
cout << "input file closed\n\n";
return numEmp; // return number employee read in file
} // end if else
} // end function readInput
/* *************************************************
* Function swap
* this function swaps to payrollStruct
* input parameters: two employee structs
* output parameters: now swapped with each other
* ************************************************* */
void swap(payrollStruct &empa, payrollStruct &empb)
{
payrollStruct temp; // place to store one employee data
temp = empb;
empb = empa;
empa = temp;
}
/* ****************************************************************
* bubble sort with flag
* this function sorts employees into order by lastname
* the by alphabetical order
* input parameters: array of payrollStruct and number of employees
* in that array
* ouput: those array items are now sorted
***************************************************************** */
void bubbleSort(payrollStruct emp[], int numE)
{
bool swapmade = false; // when true a swap was made in this pass
int i, lastpos; // last position to look for correct order
// sort the rmployees lastname
cout << "sorting employees with their last name\n";
lastpos = numE;
do
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
if (emp[i].lastname > emp[i+1].lastname) // they are in the wrong order
{ // swap all items here
swap(emp[i], emp[i+1]);
swapmade = true;
}
else
{
if (emp[i].lastname == emp[i+1].lastname) // they are in the wrong order
if (emp[i].firstname == emp[i+1].firstname) // same employees with lastname
{ // swap all items here
swap(emp[i], emp[i+1]);
swapmade = true;
}
} //end of else statement
} // end of for loop
} while(swapmade); // end of do loop
}
void findregularhours(payrollStruct emp[], int n) // find regular hours
{
for(int i=0; i<n; i++)
{
if(emp[i].hoursworked > 40) //if the hours is > 40 it will make it 40
emp[i].regularhours=40; //regular hour = 40
else
emp[i].regularhours=emp[i].hoursworked;
} //end for loop
} //regularhours()
/************************************************
* regular()
* calculates employees regular pay
************************************************/
void findregularpay(payrollStruct emp[], int n) // fnd regular pay
{
for(int i=0; i<n; i++)
{
emp[i].regularpay = emp[i].regularhours*emp[i].hourlyrate;
} //end for loop
} //regularpay
/*************************************************
* overtime()
* finds if th hours are > 40
************************************************ */
void findovertimehours(payrollStruct emp[], int n) // find overtime hours
{
for(int i=0; i<n; i++)
{
if(emp[i].hoursworked>40)
emp[i].overtimehours=emp[i].hoursworked-40; //calculates and finds how many hours are overtime
else
emp[i].overtimehours=0; //if there is no overtime
} //end for loop
} // findsovertimehours
/*************************************************
*findovertimepay()
* calculates employees overtime pay
**************************************************/
void findovertimepay(payrollStruct emp[], int n) // find overtime pay
{
for(int i=0; i<n; i++)
{
emp[i].overtimepay=emp[i].overtimehours*emp[i].hourlyrate*1.5; // calculating overtimepay
} //end for loop
} // findovertimepay
/**************************************************
* grossPay()
* calculates employees regular + overtime pay
**************************************************/
void findgrosspay(payrollStruct emp[], int n) // find the gross pay of each employee
{
for(int i=0; i<n; i++)
{
emp[i].grosspay=emp[i].regularpay+emp[i].overtimepay; // calculate gross pay
} // end for loop
} //findgrosspay
/**************************************************************************
*
*
* ************************************************************************/
void findtotalPay(payrollStruct emp[])
{
for (int i = 0; i < SZ; i++)
emp[i].totalgrossPay += emp[i].grosspay;
}
/* **************************************************************************
* output report
* this function writes data to screen and file
* input parameters: array of golfStructs and number golfers
* ouput written to screen and file womengolfreport.txt
* ************************************************************************* */
int writeReport(payrollStruct emp[],int numE) //writes report and shows payroll information
{
int i; // counter
ofstream outputFile; // output file
// open report file and write output data ********************
cout << "Report being written to file payrollreport.txt\n";
outputFile.open("payrollreport.txt"); // output file
if (outputFile.fail())
{
cout << "output file did NOT open\n";
cout << "output will only be sent to the screen\n";
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n"; //table of employees payroll data sheet to the screen
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"===========================================================================================\n";
for ( i=0; i < numE; i++)
{
cout << emp[i].firstname << " ";
cout << emp[i].lastname << " ";
cout << emp[i].empID << " ";
cout << emp[i].hoursworked << " " << emp[i].hourlyrate << " ";
cout << emp[i].regularpay << " " << emp[i].overtimepay << " ";
cout << emp[i].grosspay << " ";
cout << emp[i].totalgrossPay << " ";
}
} // end if file did not open
else
{
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n"; //table of employees payroll data sheet to the screen
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"=============================================================================================\n";
outputFile <<"First Last Employee Hours Rate Regular Overtime Gross\n";//table of employees payroll data sheet to txt file
outputFile <<"Name Name Number Worked of Pay Pay Pay Pay\n";
outputFile <<"===========================================================================================\n";
// loop to print report of employees ********************
for ( i=0; i < numE; i++)
{
// writes employee payroll data to payrollreport.txt
outputFile << setw(13) << fixed << left << emp[i].lastname << " ";
outputFile << setw(12) << emp[i].firstname << " ";
outputFile << setw(11) << emp[i].empID << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].hoursworked<< " ";
outputFile << setw(7) << right << fixed << setprecision(2) << emp[i].hourlyrate << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";
outputFile << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << "\n";
// shows employee payroll data on screen
cout << setw(14) << fixed << left << emp[i].lastname << " ";
cout << setw(12) << emp[i].firstname << " ";
cout << setw(11) << emp[i].empID << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].hoursworked<< " ";
cout << setw(7) << right << fixed << setprecision(2) << emp[i].hourlyrate << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";
cout << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << "\n";
}
cout <<"=============================================================================================\n";
cout <<"\t\t\tTotal Gross Pay\t\t\t\t\t $ " << fixed << setprecision(2) << emp[i].totalgrossPay << " \n"; //total gross pay
outputFile <<"=============================================================================================\n";
outputFile <<"\t\t\tTotal Gross Pay\t\t\t\t\t $ " << fixed << setprecision(2) << emp[i].totalgrossPay << " \n";
outputFile.close();
cout << "Output file closed\n\n";
} // end else output file open
return 0;
} // end of writeReport ************************************
*
This the result (it gives random numbers for total gross pay ** see bold sentence**
Code:
First Last Employee Hours Rate Regular Overtime Gross
Name Name Number Worked of Pay Pay Pay Pay
=============================================================================================
Adams Jane A1234 40.00 10.00 400.00 0.00 400.00
Adams John A9876 52.00 15.75 630.00 283.50 913.50
Buchanan James B7612 23.00 48.55 1116.65 0.00 1116.65
Cleveland Grover C9856 38.50 25.00 950.00 0.00 950.00
Fillmore Millard F5432 50.75 50.00 2000.00 806.25 2806.25
Grant Ulysses G5612 60.50 35.00 1400.00 1076.25 2476.25
Harrison Benjamin H5431 30.50 28.50 855.00 0.00 855.00
Hayes Rutherford H6542 45.50 35.50 1420.00 292.88 1712.88
Lincoln Mary L8765 50.00 10.00 400.00 150.00 550.00
Lincoln Abraham L9087 40.25 55.00 2200.00 20.62 2220.62
Muffett Missy M7654 42.50 12.50 500.00 46.88 546.88
Pierce Franklin P3452 40.00 45.50 1820.00 0.00 1820.00
Piper Peter P8765 30.00 10.00 300.00 0.00 300.00
Roosevelt Theodore R1234 56.50 45.25 1810.00 1119.94 2929.94
Roosevelt Elenor R2345 25.00 48.75 1218.75 0.00 1218.75
Taylor Zachary T8765 45.00 15.00 600.00 112.50 712.50
Tell William T9876 30.00 9.75 292.50 0.00 292.50
VanBuren Martin V4532 42.00 40.75 1630.00 122.25 1752.25
Washington Martha W7654 25.50 10.85 271.25 0.00 271.25
Washington George W1235 45.00 25.00 1000.00 187.50 1187.50
=============================================================================================
Total Gross Pay $ -0.01
-
December 14th, 2013, 06:41 AM
#39
Re: need help my (employee payroll program using structures)does not show all data
it gives random numbers for total gross pay
Correct! Can you explain why?
You're really not understanding what you are doing. Producing a total from some numbers displayed is not difficult - it is very easy. You obviously haven't understood my simple example in post #37.
totalgrosspay is not part of payrollStruct. It should be a local variable defined in writeReport. You don't use a function to calculate totalgrosspay.
Within writeReport()
Code:
double totalgrosspay = 0.0;
....
totalgrosspay += emp[i].grosspay;
....
cout << totalgrosspay;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 14th, 2013, 11:37 AM
#40
Re: need help my (employee payroll program using structures)does not show all data
i still dont get it as i said before im a beginner in c++
please help how to fix the code
-
December 14th, 2013, 12:28 PM
#41
Re: need help my (employee payroll program using structures)does not show all data
please help how to fix the code
I have. As this is an exercise I'm not going to give you the actual code to use. With what I've already provided you should have no difficulty. If you have, then you need to revise what you have learnt about c++ because you are not understanding it.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 14th, 2013, 12:42 PM
#42
Re: need help my (employee payroll program using structures)does not show all data
Ok just tell me how to start I take over.
-
December 14th, 2013, 01:58 PM
#43
Re: need help my (employee payroll program using structures)does not show all data
Originally Posted by Krrish1
Ok just tell me how to start I take over.
See post #39
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 14th, 2013, 02:57 PM
#44
Re: need help my (employee payroll program using structures)does not show all data
I tried but now it's gives only zero dollars I will post the code once I get home
-
December 14th, 2013, 04:34 PM
#45
Re: need help my (employee payroll program using structures)does not show all data
this is what i have:
Code:
int writeReport(payrollStruct emp[],int numE) //writes report and shows payroll information
{
int i; // counter
double totalgrosspay = 0;
ofstream outputFile; // output file
// open report file and write output data ********************
cout << "Report being written to file payrollreport.txt\n";
outputFile.open("payrollreport.txt"); // output file
totalgrosspay += emp[i].grosspay;
if (outputFile.fail())
{
cout << "output file did NOT open\n";
cout << "output will only be sent to the screen\n";
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n"; //table of employees payroll data sheet to the screen
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"===========================================================================================\n";
for ( i=0; i < numE; i++)
{
cout << emp[i].firstname << " ";
cout << emp[i].lastname << " ";
cout << emp[i].empID << " ";
cout << emp[i].hoursworked << " " << emp[i].hourlyrate << " ";
cout << emp[i].regularpay << " " << emp[i].overtimepay << " ";
cout << emp[i].grosspay << " ";
cout << emp[i].totalgrossPay << " ";
}
} // end if file did not open
else
{
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n"; //table of employees payroll data sheet to the screen
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"=============================================================================================\n";
outputFile <<"First Last Employee Hours Rate Regular Overtime Gross\n";//table of employees payroll data sheet to txt file
outputFile <<"Name Name Number Worked of Pay Pay Pay Pay\n";
outputFile <<"===========================================================================================\n";
// loop to print report of employees ********************
for ( i=0; i < numE; i++)
{
// writes employee payroll data to payrollreport.txt
outputFile << setw(13) << fixed << left << emp[i].lastname << " ";
outputFile << setw(12) << emp[i].firstname << " ";
outputFile << setw(11) << emp[i].empID << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].hoursworked<< " ";
outputFile << setw(7) << right << fixed << setprecision(2) << emp[i].hourlyrate << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";
outputFile << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";
outputFile << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << "\n";
// shows employee payroll data on screen
cout << setw(14) << fixed << left << emp[i].lastname << " ";
cout << setw(12) << emp[i].firstname << " ";
cout << setw(11) << emp[i].empID << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].hoursworked<< " ";
cout << setw(7) << right << fixed << setprecision(2) << emp[i].hourlyrate << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].regularpay << " ";
cout << setw(10) << fixed << setprecision(2) << emp[i].overtimepay << " ";
cout << setw(10) << right << fixed << setprecision(2) << emp[i].grosspay << "\n";
}
cout <<"=============================================================================================\n";
cout <<"\t\t\tTotal Gross Pay\t\t\t\t\t $ " << fixed << setprecision(2) << totalgrosspay << " \n"; //total gross pay
outputFile <<"=============================================================================================\n";
outputFile <<"\t\t\tTotal Gross Pay\t\t\t\t\t $ " << fixed << setprecision(2) << totalgrosspay << " \n"; //total gross pay
cout << "Output file closed\n\n";
} // end else output file open
return 0;
} // end of writeReport *************************************
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|