Thread: Qusetion on Class/Struct program

  1. #1
    Unregistered
    Guest

    Question Qusetion on Class/Struct program

    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();

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Worker work[WORKMAX];
    This is an awfully large local variable.
    Like around 250K bytes

    > work[i].totalIncome = work[i].income+work[i+1].income;
    Perhaps

    work[i].totalIncome = work[i].totalIncome +work[i+1].income;

    Or even

    work[i].totalIncome += work[i+1].income;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM