Please Help Me

This is a discussion on Please Help Me within the C++ Programming forums, part of the General Programming Boards category; Can someone please look at the following code and help me out ? I am trying to calculate the total ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Angry Please Help Me

    Can someone please look at the following code and help me out ?
    I am trying to calculate the total family income and size for a file of individuals. The assumption is that any person who has the same last neme as the previous person in the file is part of the same family ? I am not getting any errors but when I run the file I am getting some really strange numbers for total income and family size.

    //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].totalIncome+work[i+1].income;
    work[i].fSize++;
    cout<<work[i].lname<<" "<<work[i].totalIncome<<" "<<work[i].fSize<<endl;
    }

    return 0;

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    You didn't include your function implementation

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    4
    Implementation ? I am not quite sure what you are saying ?

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    What you included was the function prototype:
    void ReadWorkerFile(const string& fname, Worker w[], int& wCount);
    And a call to the function:
    ReadWorkerFile(fname,work,workcount);
    The implementation would be the function itself, ie:
    Code:
    //prototype
    int AddInts(int FirstInt, int SecondInt);
    
    //call to the AddInts function
    int i=AddInts(2,2);
    
    //function implementation
    int AddInts(int FirstInt, int SecondInt)
    {
        return(FirstInt+SeconInt);
    }

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    4
    //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].totalIncome+work[i+1].income;
    work[i].fSize++;
    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();

    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    Why don't you read it in and check the file for the same name at the same time.

    ie
    Code:
    ifstream inFile;
    string last_name;
    int x=0;
    float temp_income;
    string trash;  //to hold first names for they aren't needed
    
    inFile.open
    while (inFile)
    {
      inFile>>work[x].lname;
      if ((x>0)&&(work[x].lname==work[x-1].lname))
     {
        inFile >>trash;
        inFile >>temp_income;
        family_list[x-1].income+=temp_income;
       //increase the dependants for that family
     }
     else
     {
        //get the rest of the new family info
        x++; //(increase family)
     }
    }
    inFile.close();
    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    4
    How will this affect my struct ?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    like this:

    lets say the file contains this:

    Jones Mary 1555.66
    Jones John 12000.56
    Ghost Casper 444.55
    Murphy Joe 1234.77
    Murphy Jill 899999.88

    On the first loop it would read in Jones but since x is 0 it will skip ahead and read in the rest of the info (trashing the fname, and reading in the amount of the money) it will also set the dependents to 1(so you need another int in your struct)

    struct[0].fname="Jones"
    x=1

    Second loop it will read in the next line getting jones once again
    struct[1].fname="Jones" since that is the same as struct[1-1].fname it will go to the then statement.
    trashing the first name again, then adding the total income by the amount on this line, increase the dependants

    x=1

    Third loop it will read in the next line getting Ghost as a last name since this doesn't equal Jones it will read in the rest of the information at struct[x].

    So at the very end of the read
    struct[0].fname="Jones"
    struct[1].fname="Ghost"
    struct[2].fname="Murphy"
    struct[3].fname="Murphy"

    and you know how many families there is because x=3 (which is the correct number, just ignore the last struct) so pass x back so you know how many families there is.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If I can't find a run time error by reading the code I tend to throw a lot of cout statements in to monitor variable changes for several cycles of the code. Here, I would do it first in the ReadWorkerFile function to be sure everything is being read in as expected. Then I would try printing a few structs from work[] in main() to be sure they were retained in work[] appropriately, etc. At some point I can usually track down where the error is.

    In this case, I am a bit suspicious about the validity of work[]. Given that you are declaring work[] to be an array of 10000 structs, I would be concerned that you are overloading the programs stack and getting erroneous values. If that is the case, as you work through the code with appropriately placed cout statements you'll find it. The way to fix it is to use dynamic memory to declare the memory for work[] rather than store it locally on the program stack. You can do that yourself with the new and delete operators for arrays, or you can use the vector class of STL (since you are already using the string class from STL) to do the dynamic memory business for you.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    so your struct would be
    Code:
    struct Worker 
    { 
      string lname; //don't need
      string fname;  
      double income; 
      float totalIncome; //don't need
      int fSize;
    }; 
    
    const int WORKMAX = 10000;
    and as elad said make the WORKMAX smaller I think 50-100 is suffient (or less)
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21