Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //description : babbages cabbages is a program to calculate the gross pay, taxes payed and net pay for an employee given their name, hours worked and hourly pay rate.
- #include <cstdlib>
- #include <fstream>
- #include <iostream>
- #include<string>
- #include<iomanip>
- #define TAX_RATE .28 // constant to hold current tax rate
- #define MAX_REG_HOURS 40.00
- #define MIN_HOURS 20.00
- #define MAX_HOURS 60.00
- #define MIN_HOURLY_RATE 7.25
- #define MAX_HOURLY_RATE 44.99
- #define SIZE 3
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- void getEmployeeInfo(std::string& name, double& hours, double& hourlyRate);
- double getDeductions();
- void getTypeOfHours(double hours, double& regHours, double& otHours);
- double calculateGrossPay(double regHours, double otHours, double hourlyRate);
- double getSumOfGrossPay(double grossPays[],int num);
- double getAveOfGrossPay(double sum,int num);
- double calculateTaxes(double gross, double deduction);
- double calculateNetPay(double grossPay, double taxes);
- //void displayResults(std::ofstream& outfile, std::string name, double regHours,
- // double otHours, double hourlyRate, double grossPay, double netPay, double taxes, double deduction);
- //void displaySummery(std::ofstream& outfile, double grossPays[], std::string fullNames[], double sum, double ave, double minGrossPay);
- bool getYesOrNo();
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // main module to call subsequent modules
- int main()
- {
- int sum = 0,NumOfEmployees=0;
- double regHours{}, otHours{}, deduction{};
- double hours, ave = 0.0;
- double hourlyRate = 0.0;
- double grossPay = 0.0, minGrossPay{}, maxGrossPay{};
- double netPay[SIZE];
- double taxes[SIZE];
- std::string fullNames[SIZE];
- double grossPays[SIZE];
- std::ofstream outfile;
- //outfile.open("/Users/tricia/Desktop/temp.txt/payroll.txt ", std::ios::out);
- //if (!outfile.is_open()) {
- // std::cout << "Unable to open a file!" << std::endl;
- //}
- //else {
- // std::cout << " success ! " << std::endl;
- //}
- for (auto i = 0; i < SIZE; i++)
- {
- getEmployeeInfo(fullNames[i], hours, hourlyRate);
- deduction = getDeductions();
- getTypeOfHours(hours, regHours, otHours);
- grossPays[i] = calculateGrossPay(regHours, otHours, hourlyRate);
- taxes[i] = calculateTaxes(grossPays[i],deduction);
- netPay[i] = calculateNetPay(grossPays[i], taxes[i]);
- NumOfEmployees++;
- if (!getYesOrNo()) { break; }
- }
- sum = getSumOfGrossPay(grossPays,NumOfEmployees);
- ave = getAveOfGrossPay(sum,NumOfEmployees);
- minGrossPay = *std::min(std::begin(grossPays), std::begin(grossPays)+NumOfEmployees-1);
- maxGrossPay = *std::max(std::begin(grossPays), std::begin(grossPays) + NumOfEmployees-1);
- // std::cout << minGrossPay << " min\n max " << maxGrossPay; //this was for testing
- //std::cout << "display\n";
- //for (auto i = 0; i < SIZE; i++)
- // std::cout << grossPays[i] << '\n';
- //displayResults(outfile, name, regHours, otHours, hourlyRate, grossPay, netPay, taxes, deduction);
- //displaySummery(outfile, grossPays, fullNames, sum, ave, minGrossPay);
- //std::cout << "============================" << std::endl;
- //std::cout << " Employee Payroll Summery " << std::endl;
- //std::cout << "============================" << std::endl;
- //std::cout << " Name Gross pay" << std::endl;
- //std::cout << "==================== ===========" << std::endl;
- //for (int i = 0; i < SIZE; i++)
- //{
- // std::cout << std::left << std::setw(20) << fullNames[i] << " " << std::setw(11) << grossPays[i] << std::endl;
- //}
- //std::cout << "Total Gross Pay: " << sum << std::endl;
- //std::cout << "Average Gross Pay: " << ave << std::endl;
- //std::cout << "Minimum Gross Pay: " << getMinGrossPay << std::endl;
- //std::cout << "Maximum Gross Pay: " << getMaxGrossPay(grossPays) << std::endl;
- //outfile.close();
- }
- void getEmployeeInfo(std::string& name, double& hours, double& hourlyRate)
- {
- {
- std::string firstName{};
- std::string lastName{};
- while (1) {
- char c{};
- std::cout << " please enter employee's first name : " << std::endl;
- std::cin >> firstName;
- std::cout << " please enter employee's last name : " << std::endl;
- std::cin >> lastName;
- name = lastName + "," + firstName;
- std::cout << "is this correct? " << name << " y or n?\n";
- std::cin >> c;
- if (c == 'y') { break; }
- }
- std::cout << "enter employee's hours worked : " << std::endl;
- std::cin >> hours;
- while ((hours < MIN_HOURS) || (hours > MAX_HOURS))
- {
- std::cout << "The number of hours worked must be between 20 and 60 hours. " << std::endl;
- std::cin >> hours;
- }
- std::cout << "enter emplyee's current pay rate in dollars per hour : " << std::endl;
- std::cin >> hourlyRate;
- while ((hourlyRate < MIN_HOURLY_RATE) || (hourlyRate > MAX_HOURLY_RATE))
- {
- std::cout << " The hourly pay rate must be between 7.25 and 44.99. " << std::endl;
- std::cin >> hourlyRate;
- break;
- }
- }
- }
- double getDeductions()
- {
- char garage{}, transit{}, bikeLocker{};
- std::cout << " Does employee use parking garage (y/n)? ";
- std::cin >> garage;
- while ((garage != 'n') && (garage != 'N') && (garage != 'y') && (garage != 'Y'))
- {
- std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
- std::cin >> garage;
- }
- if ((garage == 'y') || (garage == 'Y'))
- {
- return 7.50;
- }
- else if ((garage == 'n') || (garage == 'N'))
- {
- std::cout << "Does the employee participate in the transit program (Y/N)?";
- std::cin >> transit; while ((transit != 'n') && (transit != 'N') && (transit != 'y') && (transit != 'Y'))
- {
- std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
- std::cin >> transit;
- }
- if ((transit == 'y') || (transit == 'Y'))
- {
- return 5.00;
- }
- else
- {
- std::cout << "Does the employee rent a bike locker (Y/N)? ";
- std::cin >> bikeLocker; while ((bikeLocker != 'n') && (bikeLocker != 'N') && (bikeLocker != 'y') && (bikeLocker != 'Y'))
- {
- std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
- std::cin >> bikeLocker;
- }
- if ((bikeLocker == 'y') || (bikeLocker == 'Y'))
- {
- return 1.00;
- }
- else { return 0.0; }
- }
- }
- }
- void getTypeOfHours(double hours, double& regHours, double& otHours)
- {
- if (hours > MAX_REG_HOURS){
- otHours = hours - MAX_REG_HOURS;
- regHours = MAX_REG_HOURS;
- }
- else{
- otHours = 0;
- regHours = hours;
- }
- }
- // module to calculate the gross pay given hours and hourly pay rate
- double calculateGrossPay(double regHours, double otHours, double hourlyRate)
- {
- return (regHours * hourlyRate) + (otHours * (hourlyRate * 1.5));
- }
- double getSumOfGrossPay(double grossPays[],int num)
- {
- int sum=0;
- for (auto i = 0; i < num; i++) {
- sum += grossPays[i];
- }
- return sum;
- }
- double getAveOfGrossPay(double sum,int num)
- {
- return sum / num;
- }
- // module to calculate taxes paid
- double calculateTaxes(double gross,double deduction)
- {
- return (gross-deduction) * TAX_RATE;
- }
- // module to use gross pay and taxes to calculate net pay
- double calculateNetPay(double grossPay, double taxes)
- {
- return grossPay - taxes;
- }
- //// module to calculate taxes paid
- //
- //// module to display results in table format.
- //void displayResults(std::ofstream& outfile, std::string name, double regHours, double otHours, double hourlyRate, double grossPay, double netPay, double taxes, double deduction)
- //{
- //
- //
- // outfile << "name Reg Hours OT hours hourly Rate gross pay deduct taxes net Pay " << std::endl;
- // outfile << "=============== ========= ======== =========== ========= ====== ===== ======= " << std::endl;
- //
- // //for(int i =0; i<SIZE; i++)
- // //{
- // outfile << std::left << std::setw(15) << name << " ";
- // outfile << std::right << std::setw(9) << regHours << " ";
- // outfile << std::setw(8) << otHours << " ";
- // outfile << std::setw(11) << hourlyRate << " ";
- // outfile << std::setw(9) << grossPay << " ";
- // outfile << std::fixed << std::setprecision(2) << std::setw(6) << deduction << " ";
- // outfile << std::setw(5) << taxes << " ";
- // outfile << std::setw(7) << netPay << std::endl;
- //}//}
- //void displaySummery(std::ofstream& outfile, double grossPays[], std::string fullNames[], double sum, double ave, double minGrossPay)
- //{
- //
- //
- // outfile << "============================" << std::endl;
- // outfile << " Employee Payroll Summery " << std::endl;
- // outfile << "============================" << std::endl;
- // outfile << " Name Gross pay" << std::endl;
- // outfile << "==================== ===========" << std::endl;
- // outfile << std::left << std::setw(22) << fullNames[0] << " ";
- // outfile << std::setw(11) << grossPays[0] << " " << std::endl;
- // outfile << std::setw(22) << fullNames[1] << " ";
- // outfile << std::setw(11) << grossPays[1] << " " << std::endl;
- // outfile << std::setw(22) << fullNames[2] << " ";
- // outfile << std::setw(11) << grossPays[2] << " " << std::endl;
- // outfile << "Total Gross Pay: " << sum << std::endl;
- // outfile << "Average Gross Pay: " << ave << std::endl;
- // outfile << "Minimum Gross Pay: " << minGrossPay << std::endl;
- //
- //}
- //
- bool getYesOrNo()
- {
- char another;
- std::cout << "Process another employee (Y/N)? : ";
- std::cin >> another;
- if (another == 'Y' || another == 'y') { return true; }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment