GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2005, 00:34
leanieleanz leanieleanz is offline
New Member
 
Join Date: Mar 2005
Posts: 10
leanieleanz is on a distinguished road
Thumbs down

Double output - Smart enough to fix this?


I seem to be getting the last record of my employees.txt file read and outputed twice. For the life of me I can't figure out why. Can someone help pleaseeeeee

CPP / C++ / C Code:
#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 

using namespace std; 

void handleData(int& id, char& empType, double& empRate, double& emp401K, int& empDependents, 
                double& empYTDHours, double& empYTDGross, string& empName, char def, double& federal, 
                double& state, double& FICA, double& medicare, double& depDeduct, double& FICALimit, 
                int hoursId, double hours, double& gross, double hEmpTotal, double sEmpTotal, 
                double& totalFederal, double& totalState, double& totalFICA, double& totalMedicare, 
                double& retirement, double& totalDependents, double& net, ifstream& Employee, 
                ifstream& Hours, ifstream& Taxes, ofstream& Register); 

void calculations(double& gross, double& emp401K, int& empDependents, double& depDeduct, double& federal, 
                  double& state, double& FICA, double& medicare, double& retirement, 
                  double& totalDependents, double& totalFederal, double& totalState, double& totalFICA, 
                  double& totalMedicare, double& net); 

int main() 
{ 
    //declare variables 
    int id = 0; 
    char empType = ' '; 
    double empRate = 0; 
    double emp401K = 0; 
    int empDependents = 0; 
    double empYTDHours = 0; 
    double empYTDGross = 0; 
    string empName = "hello"; 
    double hours = 0; 
    char def = ' '; 
    double federal = 0; 
    double state = 0; 
    double FICA = 0; 
    double medicare = 0; 
    double FICALimit = 0; 
    double depDeduct = 0; 
    int hoursId = 0; 
    double gross = 0; 
    double hEmpTotal = 0; 
    double sEmpTotal = 0; 
    double totalFederal = 0; 
    double totalState = 0; 
    double totalFICA = 0; 
    double totalMedicare = 0; 
    double totalDependents = 0; 
    double retirement = 0; 
    double net = 0; 

    //Declare infiles and outfile 
    ifstream Employee;  //input employee information 
    ifstream Hours; // input hours file 
    ifstream Taxes; // input taxes file 
    ofstream Register; //output file 

    Employee.open("e://Employee.txt"); 
    Hours.open("e://Hours.txt"); 
    Taxes.open("e://Taxes.txt"); 
    Register.open("e://Register.txt"); 

    Register<< fixed << showpoint; 
    Register << setprecision(2); 

    handleData(id, empType, empRate, emp401K, empDependents, empYTDHours, empYTDGross, empName, def, 
               federal, state, FICA, medicare, depDeduct, FICALimit, hoursId, hours, gross, 
               hEmpTotal, sEmpTotal, totalFederal, totalState, totalFICA, totalMedicare, retirement, 
               totalDependents, net, Employee, Hours, Taxes, Register); 

    return 0; 
} 

void handleData(int& id, char& empType, double& empRate, double& emp401K, int& empDependents, 
                double& empYTDHours, double& empYTDGross, string& empName, char def, double& federal, 
                double& state, double& FICA, double& medicare, double& depDeduct, double& FICALimit, 
                int hoursId, double hours, double& gross, double hEmpTotal, double sEmpTotal, 
                double& totalFederal, double& totalState, double& totalFICA, double& totalMedicare, 
                double& retirement, double& totalDependents, double& net, ifstream& Employee, 
                ifstream& Hours, ifstream& Taxes, ofstream& Register) 
{ 
     
    cout << "Processing data..." << endl; 

    while (Taxes) 
    { 
        Taxes >> def; 
            //import tax and percentages in the same order of the given Taxes file 
            if(def == 'F') 
            { 
            Taxes >> federal; 
            } 
            else if(def == 'S') 
            { 
            Taxes >> state; 
            } 
            else if(def == 'R') 
            { 
            Taxes >> FICA; 
            } 
            else if(def == 'M') 
            { 
            Taxes >> medicare; 
            } 
            else if(def == 'D') 
            { 
            Taxes >> depDeduct; 
            depDeduct = depDeduct / 52; 
            } 
            else if(def == 'L') 
            { 
            Taxes >> FICALimit; 
            } 
    } //end while 

    Register << left; 
    Register << setw(8) << "ID" << setw(29) << "Name" << setw(11) << "Gross" << setw(9) << "Federal" 
             << setw(9) << "State" << setw(9) << "FICA" << setw(9) << "Medicare" << setw(13) 
             << "Retirement" << setw(10) << "Net Pay" << setw(10) << endl << endl; 

    while (Employee && Hours) //This is where I pull the data in from the files 
    { 
        Employee >> id >> empType >> empRate >> emp401K >> empDependents >> empYTDHours >> empYTDGross; 
        getline(Employee, empName); 

        Hours >> hoursId >> hours; 

        if(id == hoursId) //make sure the id's in the employee and hours file match up 
        { 
            if(empType == 'H') //calculate overtime for hourly employees if applicable 
            { 
                if(hours <= 40) //no overtime 
                { 
                    gross = empRate * hours; 
                    hEmpTotal = gross + hEmpTotal; 
                } //end 2nd inner if 

                else //employees that qualify for overtime pay 
                { 
                    gross = hours - 40; 
                    gross = (empRate/2 + empRate) * gross; //Time and a half 
                    gross = empRate * 40 + gross; 
                } //end else 
            }//end 1st inner if 

            else if(empType == 'S') 
            { 
                gross = empRate / 52; 
                sEmpTotal = gross + sEmpTotal; 
            }//end else if 

        if(FICALimit <=(empYTDGross * FICA)) 
        { 
            FICA = 0; //The FICA maximum has been met 
        } 
         
    }//end outer if 

    calculations(gross, emp401K, empDependents, depDeduct, federal, state, FICA, medicare, retirement, 
                 totalFederal, totalState, totalFICA, totalMedicare, totalDependents, net); 
     
    Register << left; //This is where I output the variables to the output file.  I get my last record outputted twice. 
    Register << setw(5) << id << setw(30) << empName << "$" << setw(10) << gross << "$" << setw(8) 
             << totalFederal << "$" << setw(8) << totalState << "$" << setw(8) << totalFICA << "$" 
             << setw(8) << totalMedicare << "$" << setw(12) << retirement << "$" << setw(8) << net 
             << setw(10) << endl; 
    Register << endl; 

    cout << fixed << showpoint; 
    cout << setprecision(2); 
    cout << left; 
    cout << " _________________________________________________________________" << endl; 
    cout << "\n| "<< setw(35) << id << setw(30) << empName << endl; 
    cout << "|" << endl; 
    cout << "| Deductions" << endl; 
    cout << "| Federal Tax: $" << setw(24) << totalFederal << "YTD Hours: " << empYTDHours << endl; 
    cout << "| State Tax:   $" << setw(24) << totalState << "YTD Gross: " << empYTDGross << endl; 
    cout << "| FICA:        $" << setw(24) << totalFICA << "Dependents: " << empDependents << endl; 
    cout << "| Medicare:    $" << totalMedicare << endl; 
    cout << "| 401K:        $" << retirement << endl; 
    cout << "|\t \t \t \t \tGross Pay: $" << empYTDGross << endl; 
    cout << "|\t \t \t \t \tNet Pay:   $" << net << endl; 
    cout << "_________________________________________________________________" << endl; 

    }//end while 

    Register << "TOTALS: " << endl; 
    Register << "Hourly Employees: $" << hEmpTotal << endl; //hourly employees first 
    Register << "Salary Employees: $" << sEmpTotal << endl; 
    Register << "\nGRAND TOTAL: $" << (hEmpTotal + sEmpTotal) << endl; 

    Employee.close(); 
    Hours.close(); 
} 

void calculations(double& gross, double& emp401K, int& empDependents, double& depDeduct, double& federal, 
                  double& state, double& FICA, double& medicare, double& retirement, double& totalFederal, 
                  double& totalState, double& totalFICA, double& totalMedicare, double& totalDependents, 
                  double& net) 
{ 
    totalFederal = gross * federal; 
    totalState = gross * state; 
    totalFICA = gross * FICA; 
    totalMedicare = gross * medicare; 
    retirement = gross * emp401K; 
    totalDependents = empDependents * depDeduct; 
    net = gross + totalDependents - totalFederal - totalState - totalFICA - totalMedicare - retirement; 
} 
Last edited by LuciWiz : 11-Mar-2005 at 00:55. Reason: Please insert your C++ code between [c++ ] & [/c++] tags
  #2  
Old 11-Mar-2005, 21:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
It took me way too long to figure out where your problem was -- you don't need to post your entire program, just the part you need help with. Your problem is
CPP / C++ / C Code:
while (Employee && Hours) //This is where I pull the data in from the files 
{ 
    Employee >> id >> empType >> empRate >> emp401K >> empDependents >> empYTDHours >> empYTDGross; 
    getline(Employee, empName); 
After you read the last record, you are assuming the input streams are now FALSE. T'ain't so. They are FALSE only after you try to read one more record. But after that read the last record is still in the variables and you simply continue processing. Try:
CPP / C++ / C Code:
while (1) // start a 'forever' loop 
{ 
    Employee >> id >> empType >> empRate ...; 
    getline(Employee, empName); 
    if (!Employee) break;  // If error, done...
There are better ways to handle the problem, but this will at least get you going (and thinking, I hope).

Also, in 10 posts you have been asked to use CODE tags 5 times. Please comply. LuciWiz and JDS are probably getting tired of editing your posts for you.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick help - redefinition error leanieleanz C++ Forum 1 08-Mar-2005 01:32
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 09:09
No output from loop function crystalattice C++ Forum 2 20-Dec-2004 21:39
"undefined symbol" error crystalattice C++ Forum 3 27-Sep-2004 08:32
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:13.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.