0

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

Here is the output, notice that the math is off by 1 cent

SeeLamb
  • 3
  • 2
  • 3
    Without looking more carefully at your code I can't be _sure_ that ["is floating point math broken?"](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) is relevant, but it's an important read regardless. – Nathan Pierson Apr 02 '22 at 23:47
  • 3
    Does this answer your question? [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) Unfortunately, switching to proper fixed-point arithmetic will require rewriting almost everything you have so far. – Nate Eldredge Apr 02 '22 at 23:50
  • 1
    Definitely change your program to use integers, and store your cash values in pennies (e.g. to represent $10 you'd say `int value = 1000;`). Once you've done that, you'll get the mathematical behavior you want, and the only remaining task it to write a function that prints out the integer value the way a human would expect to see it (it can use division-by-100 to generate the dollar value and modulo-by-100 to generate the cents value) – Jeremy Friesner Apr 03 '22 at 00:02

0 Answers0