#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct EmployeeData
{
string employeeName;
float overtime;
float grossPay;
float hoursWorked;
float hourlyRate;
float statetaxOwed;
float statetaxRate;
float fedtaxOwed;
float fedtaxRate;
float netPay;
float totalgp;
float totalft;
float totalst;
float totalnp;
};
EmployeeData employee[4]; //array of 4 employees
void calculate_stats(EmployeeData& employee);
int main()
{
for (int i = 0; i < 4; ++i)
{
cout << "Please enter Employee #" << (i+1) << "'s" << " name: ";
cin.ignore();
getline(cin, employee[i].employeeName);
cout << "Please enter Employee #" << (i+1) << "'s hours worked: ";
cin >> employee[i].hoursWorked;
cout << "Please enter Employee #" << (i+1) << "'s hourly rate: ";
cin >> employee[i].hourlyRate;
cout << "Please enter Employee #" << (i+1) << "'s Federal Tax Rate: ";
cin >> employee[i].fedtaxRate;
cout << "Please enter Employee #" << (i+1) << "'s State Tax Rate: ";
cin >> employee[i].statetaxRate;
cout << endl;
}
for (int i = 0; i < 4; ++i)
{
calculate_stats(employee[i]);
cout << setprecision(2) << showpoint << fixed;
cout << "Employee #" << (i+1) << "'s name is: " << employee[i].employeeName << endl;
cout << "Employee #" << (i+1) << "'s Gross Pay is: $" << employee[i].grossPay << endl;
cout << "Employee #" << (i+1) << "'s Federal Taxes owed is: $" << employee[i].fedtaxOwed << endl;
cout << "Employee #" << (i+1) << "'s State Taxes owed is: $" << employee[i].statetaxOwed << endl;
cout << "Employee #" << (i+1) << "'s Net Pay is: $" << employee[i].netPay << endl;
cout << endl;
}
cout << "Total Gross Pay: " << employee[4].totalgp << endl; //here is the problem
cout << "Total Federal Tax Owed: " << employee[4].totalft<< endl;
cout << "Total State Tax Owed: " << employee[4].totalft<< endl;
cout << "Total Net Pay: " << employee[4].totalft << endl;
}
void calculate_stats(EmployeeData& employee)
{
if (employee.hoursWorked>40) {
employee.hoursWorked = ((employee.hoursWorked-40) * (1.5)) + 40;
}
else {
employee.hoursWorked = employee.hoursWorked;
}
employee.grossPay = employee.hoursWorked * employee.hourlyRate;
employee.fedtaxOwed = employee.grossPay * (employee.fedtaxRate/100);
employee.statetaxOwed = employee.grossPay * (employee.statetaxRate/100);
employee.netPay = (employee.grossPay - employee.fedtaxOwed- employee.statetaxOwed);
employee.totalgp = employee.totalgp + employee.grossPay;
employee.totalft = employee.totalft + employee.fedtaxOwed;
employee.totalst = employee.totalst + employee.statetaxOwed;
employee.totalnp = employee.totalnp + employee.netPay;
}
for-loopsの外側にあるが、まだメインにあるcoutブロックを確実に含めることができました。計算統計を使用しても私の出力は変わりませんでした。アウトプットは私にすべて0.00を与えています...彼らは4人の従業員全員で完全な合計でなければなりません(従業員4人全員の総賃金が1000の場合、総支払額は4000でなければなりません。ここでメイン内の配列から総計を印刷する方法
Total Gross Pay: 0.00
Total Federal Tax Owed: 0.00
Total State Tax Owed: 0.00
Total Net Pay: 0.00
Press any key to continue . . .
は、私はこれをどのように修正すればよい...(、残りは細かい誤った表示のスナップショット作品)の出力は次のようになりますemployee.totalgp = employee.totalgp + employee.grossPay;
ありがとう
'employee [4]'は存在しません。それにアクセスすることは未定義の動作です。 –
変数から '[4]'を削除するとエラーが出ます。変数から 'employee [4] .'を削除すると、エラーも出ます。私がそれにアクセスできない場合、すべての従業員の価値を得るためには、どのようにアクセスするべきですか? – MichelleZ