该程序应该使用单个函数来返回多个值。但是,由于函数一次只能返回一个值,因此我实现了一个开关。但是,当我运行代码时,总工资,税金和净工资默认为0而不是加起来。有谁知道为什么会这样?我只允许使用带开关的单个函数来返回不同的值。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
double paycalculations(double, double, double, double, int);
double paycalculations(double hrs, double pay, double fedrate, double staterate, int option)
{
switch (option)
{
case 1:
double reg;
if (hrs < 0)
{
hrs = hrs * (-1);
}
if (hrs <= 40)
{
reg = hrs * pay;
}
else
{
reg = 40 * pay;
}
return reg;
break;
case 2:
double overtime;
overtime = (hrs - 40) * (1.5 * pay);
return overtime;
break;
case 3:
double gross;
if (overtime > 0)
{
gross = reg + overtime;
}
else
{
gross = reg;
}
return gross;
break;
case 4:
double ftaxes;
ftaxes = gross * fedrate;
return ftaxes;
break;
case 5:
double staxes;
staxes = gross * staterate;
return staxes;
break;
case 6:
double socialsectaxes;
socialsectaxes = gross * 0.062;
return socialsectaxes;
break;
case 7:
double medtaxes;
medtaxes = gross * 0.0145;
return medtaxes;
break;
case 8:
double net;
net = gross - (ftaxes + staxes + socialsectaxes + medtaxes);
return net;
break;
}
}
int main ()
{
string employeeID;
double hours, payrate, fedtaxrate, statetaxrate, regpay, overtimepay, grosspay, fedtaxes, statetaxes, sosectaxes, meditaxes, netpay;
const int SIZE = 100;
char again = 'y', fullname[SIZE] = {" "};
while (again == 'y')
{
hours = payrate = fedtaxrate = statetaxrate = regpay = overtimepay = fedtaxes = statetaxes = sosectaxes = meditaxes = netpay = 0;
system("reset");
cout <<"Employee Full Name: ";
cin.getline(fullname, SIZE);
cout << "\nEmployee ID: ";
cin >> employeeID;
cout << "\nHours Worked (Maximum of 60): ";
cin >> hours;
cout << "\nEmployee Payrate: $";
cin >> payrate;
cout << "\nFederal Tax Rate (Maximum of 0.5 or 50%): ";
cin >> fedtaxrate;
cout << "\nState Tax Rate (Maximum of 0.3 or 30%): ";
cin >> statetaxrate;
cout.setf(ios::fixed | ios::showpoint);
system("reset");
cout << "\t\t\t\t\tPayroll";
cout << "\n\nEmployee ID: " << employeeID;
cout << "\t\nEmployee Name: " << fullname;
cout.precision(1);
cout << "\n\nTotal Hours Worked: " << "\t\t\t\t" << hours;
cout.precision(2);
regpay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 1);
if (hours <= 40)
{
cout << "\nRegular Hours Worked: " << hours << " @ $" << payrate << "/hour" << "\t" << "$" << regpay;
}
overtimepay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 2);
if (hours > 40)
{
cout << "\nRegular Hours Worked: " << "40.00 @ $" << payrate << "/hour" << "\t" << "$" << regpay;
cout << "\nOvertime Hours Worked: " << (hours - 40) << " @ $" << (payrate * 1.5) << "/hour" << "\t" << "$" << overtimepay;
}
grosspay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 3);
cout << "\nGross Pay: " << "\t\t\t\t\t" << "$" << grosspay;
fedtaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 4);
cout << "\nFederal Taxes Withheld @" << (fedtaxrate * 100) << "%" << "\t\t\t" << "$" << fedtaxes;
statetaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 5);
cout << "\nState Taxes Withheld @" << (statetaxrate * 100) << "%" << "\t\t\t" << "$" << statetaxes;
sosectaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 6);
cout << "\nSocial Security Withheld @" << (0.062 * 100) << "%" << "\t\t\t" << "$" << sosectaxes;
meditaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 7);
cout << "\nMedicare Withheld @" << (0.0145 * 100) << "%" << "\t\t\t" << "$" << meditaxes;
netpay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 8);
cout << "\nNet Pay: " << "\t\t\t\t\t" << "$" << netpay;
cout << "\n\nWould you like to compute another employee's pay check? (Y or N): ";
cin >> again;
again = tolower(again);
}
}