I am working on a program that is designed to read in a group of names and incomes and compute the total family income. It is assumed that two consectutive names in the file with the same last name are one family. Below is the code that I have written. Although, I am not receiving any errors the program is not rolling up the incomes into the total family income.Could someone please let nme know if they can see what is going wrong here ?
/familyTaxfile.cpp - reads in names of individuals, sorts alphabetically
//by the person's last name and calculates total income fpr a household and
//any applicable tax
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
// Worker Type Definition
struct Worker {
string lname;
string fname;
double income;
float totalIncome;
int fSize;//size of family
};
const int WORKMAX = 10000;
void ReadWorkerFile(const string& fname, Worker w[], int& wCount);
int main()
{
Worker work[WORKMAX];
int workcount;
int i,j;
string fname;
cout<<"Enter the name of the file > ";
cin>>fname;
ReadWorkerFile(fname,work,workcount);
for (i = 0; i < workcount - 1; i++)
for (i = 0; i < workcount - 1; i++)
for(j = 0; j <workcount-1;j++)
if(work[j].lname>work[j+1].lname){
Worker tmp = work[j];
work[j] = work[j+1];
work[j+1]=tmp;
}
for (i = 0; i < workcount; i++)
cout<<work[i].lname<<" "<<work[i].fname<<" $"<<work[i].income<<endl;
for (i = 0; i < workcount - 1; i++)
if(work[i].lname == work[i+1].lname){
work[i].totalIncome = work[i].income+work[i+1].income;
work[i].fSize++;
cout<<work[i].lname<<" "<<totalIncome<<" "<<fSize<<endl;
}
cout<<work[i].lname<<" "<<work[i].totalIncome<<" "<<work[i].fSize<<endl;
return 0;
}
void ReadWorkerFile(const string& fname, Worker w[], int& wCount)
{
ifstream f;
Worker tmp;
f.open(fname.c_str());
if (f.fail()) {
cerr<<"Cannot open input file "<<fname<<endl;
return;
}
wCount= 0;
while(f>>tmp.lname>>tmp.fname>>tmp.income){
w[wCount] = tmp;
wCount++;
}
f.close();
}