Let me start by saying I am a computer science student in my first semester. Our first project is a simple one but there is some mathematical error that I can't figure out. My math is off by 1 cent in the sample output that was provided. I'm sure it has something to do with the decimal places for certain variables. Any help is appreciated. Thank you. Program is written in C++.
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
// Declare constants
const double BASE_HOURS = 40.00;
const double OT_MULTIPLIER = 1.50;
// Function prototypes
void programIntro();
void inputData(string& name, double& hours, double& rate);
void invalidOutput(string name, double hours, double rate);
void calcGrossPay(double hours, double rate, double& gross);
void calcPayWithOT(double hours, double rate, double& gross);
void calcRegularPay(double hours, double rate, double& gross);
void calcDeductions(double gross, double& socialTax, double& fedTax, double& staTax);
void calcNetPay(double& net, double gross, double socialTax, double fedTax, double staTax);
void displayData(string name, double hours, double rate, double gross, double socialTax, double fedTax, double staTax, double net);
int main()
{
// Declare variables
string employeeName;
double hoursWorked;
double hourlyRate;
double grossPay;
double socialSecurityTax;
double federalTax;
double stateTax;
double netPay;
// Call introduction module. No arguments necessary
programIntro();
// Call input module to collect user input to appropriate variables
inputData(employeeName, hoursWorked, hourlyRate);
// Determine if hourly rate meets minimum wage requirement
// If not, calculate pay
if (hourlyRate < 10)
invalidOutput(employeeName, hoursWorked, hourlyRate);
else
{
calcGrossPay(hoursWorked, hourlyRate, grossPay);
calcDeductions(grossPay, socialSecurityTax, federalTax, stateTax);
calcNetPay(netPay, grossPay, socialSecurityTax, federalTax, stateTax);
displayData(employeeName, hoursWorked, hourlyRate, grossPay, socialSecurityTax, federalTax, stateTax, netPay);
}
cout << endl << "Thank you for using the program." << endl << endl << endl;
return 0;
}
// Introduction module
void programIntro()
{
cout << "Hello! I wrote this program to help you calculate the pay and withholdings for each employee.\n";
cout << "All you need to do is input the employee's name, number of hours worked, and his/her hourly rate.\n";
cout << "Let's get started!\n\n";
}
// Input module
void inputData(string& name, double& hours, double& rate)
{
cout << "Please enter the first and last name of the employee: ";
getline(cin, name);
cout << "Please enter the number of hours worked: ";
cin >> hours;
cout << "Please enter the hourly rate: $";
cin >> rate;
}
// Invalid wage module message
void invalidOutput(string name, double hours, double rate)
{
cout << fixed << showpoint << setprecision(2);
cout << endl << name << " worked " << hours << " hours with an hourly rate of $" << rate << "." << endl;
cout << endl << "Input Error: Below minimum wage of $10.00\n";
cout << "Unable to provide data for ineligible employee.\n";
cout << "Please try again." << endl << endl;
}
// Gross pay module
void calcGrossPay(double hours, double rate, double& gross)
{
if (hours > BASE_HOURS)
calcPayWithOT(hours, rate, gross);
else
calcRegularPay(hours, rate, gross);
}
// Gross pay with overtime module
void calcPayWithOT(double hours, double rate, double& gross)
{
double overtimeHours;
double overtimePay;
overtimeHours = hours - BASE_HOURS;
overtimePay = overtimeHours * rate * OT_MULTIPLIER;
gross = BASE_HOURS * rate + overtimePay;
}
// Gross pay without overtime module
void calcRegularPay(double hours, double rate, double& gross)
{
gross = hours * rate;
}
// Calculate deductions module
void calcDeductions(double gross, double& socialTax, double& fedTax, double& staTax)
{
socialTax = gross * 0.085;
fedTax = gross * 0.10;
staTax = gross * 0.05;
}
// Net pay module
void calcNetPay(double& net, double gross, double socialTax, double fedTax, double staTax)
{
double totalDeductions;
totalDeductions = socialTax + fedTax + staTax;
net = gross - totalDeductions;
}
// Final output module
void displayData(string name, double hours, double rate, double gross, double socialTax, double fedTax, double staTax, double net)
{
cout << fixed << showpoint << setprecision(2);
cout << endl << name << " worked " << hours << " hours with an hourly rate of $" << rate << ".\n";
cout << "Weekly salary statement for " << name << ":\n\n";
cout << setw(15) << "Gross Pay:" << setw(18) << "$" << setw(10) << gross << endl;
cout << setw(25) << "Social Security Tax:" << setw(8) << "$" << setw(10) << socialTax << endl;
cout << setw(17) << "Federal Tax:" << setw(16) << "$" << setw(10) << fedTax << endl;
cout << setw(15) << "State Tax:" << setw(18) << "$" << setw(10) << staTax << endl;
cout << setw(43) << "--------------------------------------" << endl;
cout << setw(13) << "Net Pay:" << setw(20) << "$" << setw(10) << net << endl << endl;
}