所以我完成了98%的代码,但我遇到了一个问题,而不是一个总和,我得到五个单独的代码。我怎么会得到一个总数?我的任务是获得5个不同员工的总工资单,这些员工以文本文件的形式存在。 供参考文本代码为:
123-45-6789,Linda Smith,50,10.00,133-56-7890,Carol Melville,35,12.50,167-33-4589,Marilyn Lloyd,42,16.00, 156-90-5432,Joyce Davis,27,11.25,195-56-9000,Susan Cook,49,12.00
这是实际的程序:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct incomeInfo {
string id;
string name;
int hours;
double hRate;
double regPay = 0;
double otPay = 0;
};
const int ARRAY_SIZE = 25; // Array size
incomeInfo income[ARRAY_SIZE]; // array variable declaration
void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);
int main()
{
incomeInfo income[ARRAY_SIZE];
int count = 0; //Initialize count to 0
getIncome(income, count);
compute(income, count);
display(income, count);
summary(income, count);
return 0;
}
void getIncome(incomeInfo income[], int &count)
{
string name;
char line[50]; // Variable to read data
// Open data File to read data
ifstream inputFile;
string fileName;
int value;
cout << "Please enter the path name where payroll.txt is located (Check the HW5 folder): ";
getline(cin, fileName);
// Open the file
inputFile.open(fileName.c_str());
// test if data file opened incorrectly
if (inputFile.fail())
{
cout << "\n\n\tError openning file." << "\n\n\t";
system("pause");
exit(1);
}
else
{
while (!inputFile.eof()) //Check end of file
{
inputFile.getline(line, 50, ','); // The data are separated by comma
income[count].id = line;
inputFile.getline(line, 50, ',');
income[count].name = line;
inputFile.getline(line, 50, ',');
income[count].hours = atoi(line); // Convert string to integer
inputFile.getline(line, 50, ',');
income[count].hRate = atof(line); // Convert string to float
count++;
}
}
inputFile.close();
return;
}
void compute(incomeInfo *ptrI, int count)
{
for (int i = 0; i<count; i++)
{
if (ptrI->hours <= 40)
{
ptrI->regPay = ptrI->hours * ptrI->hRate;
ptrI++;
}
else if (ptrI->hours > 40)
{
//ptrI->regPay = ptrI->hours * ptrI->hRate;
ptrI->regPay = 40 * ptrI->hRate;
ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
ptrI++;
}
}
return;
}
void display(incomeInfo income[], int count)
{
cout << fixed << showpoint << setprecision(2);
cout << setw(15) << left << "ID" << setw(16) << "Name";
cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" << endl;
for (int i = 0; i < count; i++)
{
cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
}
return;
}
void summary(incomeInfo income[], int count)
{
for (int i = 0; i < count; i++)
cout << endl << endl << "Total payroll amount for the company = " << income[i].regPay + income[i].otPay << endl;
system("PAUSE");
}