计算没有处理C++

Calculations no processing C++

本文关键字:C++ 处理 计算      更新时间:2023-10-16

我正在制作一个简单的工资计算器,由于某种原因,我的"税"没有计算。 当我运行程序时,它们只在输出中显示 $0。 知道是什么原因造成的吗? 我评论说"//不计算。在未计算的行上显示 $0"。

#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns:    Zero
// Calls:      None
{
int const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05, union_dues = 10;
int       gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
    do
    {
        cout << "nnEnter the number of hours the employee worked: " << endl;
        cin >> hours_worked;
        if (hours_worked > 40) {
            overtime_pay = (hours_worked - 40) * overtime_rate;
            gross_pay = (40 * hourly_wage) + overtime_pay;
        }
        else {
            gross_pay = hours_worked * hourly_wage;
        }
        cout << "nnEnter the number of dependents for employee: " << endl;
        cin >> number_of_dependents;
            if (number_of_dependents >= 3) {
                insurance = 35;
            }
            else {
                insurance = 0;
            }
            // Payroll Calculation
            ss_withheld = gross_pay * social_security;
            federal_withheld = gross_pay * federal_income;
            state_withheld = gross_pay * state_income;
            net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
            cout << "nnGross Pay:                 $" << gross_pay << endl;
            cout << "Social Security Withhholding:  $" << ss_withheld << endl; //  Not calculating. Shows $0
            cout << "Federal Withholding:           $" << federal_withheld << endl; //  Not calculating. Shows $0
            cout << "State Withholding:             $" << state_withheld << endl; //  Not calculating. Shows $0
            cout << "Union Dues:                    $" << union_dues << endl;
            cout << "Insurance Deduction:           $" << insurance << endl;
            cout << "Net Pay:                       $" << net_pay << endl;
            cout << "nDo you want to do another employee(Y/N)? ";
            cin >> again;
    } while (again == 'y' || again == 'Y');
    cout << "nHope they enjoy the money!" << endl;
    return 0;

}

看起来您正在为双精度值创建 int Const

。 即。
    #include "stdafx.h";
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns:    Zero
// Calls:      None
{
    int const union_dues = 10;    
    double const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05;
    double  gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;

    cout << "This program will calculate payroll for an employee." << endl;
    do {
        cout << "nnEnter the number of hours the employee worked: " << endl;
        cin >> hours_worked;
        if (hours_worked > 40) {
            overtime_pay = (hours_worked - 40) * overtime_rate;
            gross_pay = (40 * hourly_wage) + overtime_pay;
        }
        else {
            gross_pay = hours_worked * hourly_wage;
        }
        cout << "nnEnter the number of dependents for employee: " << endl;
        cin >> number_of_dependents;
        if (number_of_dependents >= 3) {
            insurance = 35;
        }
        else {
            insurance = 0;
        }
        // Payroll Calculation
        ss_withheld = gross_pay * social_security;
        federal_withheld = gross_pay * federal_income;
        state_withheld = gross_pay * state_income;
        net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
        cout << "nnGross Pay:                 $" << gross_pay << endl;
        cout << "Social Security Withhholding:  $" << ss_withheld << endl; //  Not calculating. Shows $0
        cout << "Federal Withholding:           $" << federal_withheld << endl; //  Not calculating. Shows $0
        cout << "State Withholding:             $" << state_withheld << endl; //  Not calculating. Shows $0
        cout << "Union Dues:                    $" << union_dues << endl;
        cout << "Insurance Deduction:           $" << insurance << endl;
        cout << "Net Pay:                       $" << net_pay << endl;
        cout << "nDo you want to do another employee(Y/N)? ";
        cin >> again;
    } while (again == 'y' || again == 'Y');
    cout << "nHope they enjoy the money!" << endl;
    return 0;
}