第一次在这里发布。问题要求使用函数来计算并将总工资,净工资等工资单输出到表中,并将条件输出到文本文件中。但是我的问题是当我使用数组时函数拒绝循环。
#include <iostream>
#include <fstream>//File stream
#include <string> //For use with string variables
#include <iomanip>//For use with formatting
double hoursworked[6],hourlyrate[6];
int i;
using namespace std;
double gross_fun(double hourlyrate[], double hoursworked[],int &numofelements)
{
double overtime[6];
double gross[6];
for(i=0; i<=5; i++){
if (hoursworked[i] > 40){
overtime[i] = 1.5*hourlyrate[i] * (hoursworked[i]-40); //Overtime is 1.5 times hourly rate
gross[i]=overtime[i]+(40*hourlyrate[i]);
}
else
gross[i]= hourlyrate[i] * hoursworked[i];
return gross[i];
}
}
double taxes_fun(double gross[], int &numofelements)
{
double taxes;
for(i=0; i<=5; i++){
taxes = .1 * gross[i];
return taxes;
}
}
double SS_fun( double gross[], int &numofelements)
{
double social;
for(i=0; i<=5; i++){
social = .05 * gross[i];
return social;
}
}
double netpay_fun(double gross[], double tax[], double social[], int &numofelements)
{
double netpay;
for(i=0; i<=5; i++){
netpay= gross[i] - (tax[i] + social[i]); //Resultant net pay given when taxes and security are subtracted
return netpay;
}
}
double taxes(double tax[], int &numofelements);
double social_security (double social[], int &numofelements);
double netpay_fun (double netpay[], int &numofelements);
double gross_fun(double hourlyrate[], double hoursworked[], int &numofelements);
int main ()
{
ifstream inFile; //To read .txt file
inFile.open ("input.txt");
if (inFile.fail()){
cerr << "Error Opening File" << endl;
exit(1);
}
int EmpNum[6];//Employee Number
int i = 0; //Counter
double gross[6],taxes[6],socia[6],netpay[6],moneytopay[6];
double overtime[6],hoursworked[6],hourlyrate[6],social[6],totalnetpay = 0.0f;
char paytype[6];
string firstname[6],lastname[6];
cout << setprecision(2) << fixed; //Set double values to two decimal places
(inFile>>EmpNum[i]>>firstname[i]>>lastname[i]>>hoursworked[i]>>hourlyrate[i]>>paytype[i]){
i++;
}
for(i=0;i<=5;i++){
gross[i]=gross_fun(hourlyrate, hoursworked, i);
taxes[i] = taxes_fun(gross, i);
social[i] = SS_fun (gross, i);
netpay[i] = netpay_fun (gross, taxes, social, i);
totalnetpay=totalnetpay+netpay[i];
}
}
cout << setw(6) << "EmpNo"<< setw(13) << "First Name"<< setw(13) << "Last Name"<< setw(8) << "Gross";
cout << setw(8) << "Tax"<< setw(8) << "SS"<< setw(10) << "Net Pay"<< setw(9) << " Payment Type";
for(i=0;i<6;i++){
cout << setw(5) <<EmpNum[i]<< setw(11) <<firstname[i]<<setw(14) <<lastname[i]<< setw(11) << gross[i]<< setw(8) << taxes[i];
cout << setw(9) << social[i]<< setw(8) << netpay[i]<< setw(7) << paytype[i]<< endl;
}
cout<<"\nSum of netpay: "<<totalnetpay;
return 0;
}
原因很简单,您已将return
语句放入for
循环中。只需将其移出即可。
double gross_fun(double hourlyrate[], double hoursworked[],int &numofelements) //Function to determine gross salary
{
double overtime[6];
double gross[6];
double total = 0.0;
for(i=0; i<=5; i++){
if (hoursworked[i] > 40){ //Calculating gross salary if hours worked greater than 40
overtime[i] = 1.5*hourlyrate[i] * (hoursworked[i]-40); //Overtime is 1.5 times hourly rate
gross[i]=overtime[i]+(40*hourlyrate[i]);
}
else
gross[i]= hourlyrate[i] * hoursworked[i]; //Calculating gross salary if no overtime is done
// remove here
}
return // something
}
您已在所有函数中完成了相同的操作。如果您想返回一个数组,我建议您在参数中传递它并返回void。或返回一个数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句